python中的valueerror_【已解决】python中json.loads出错:ValueError: Expecting property name: line 1 column 9 (ch...

【问题】

别人给了一个json(形式)的,非正常的json字符串{data:{{tg_i":"1080723", contest_i":"1001", sid:"1567515287"}, {tg_id:"1080723", contest_id:"1001",sid:"1567515287"}}, total: "2"}

格式化后为:{

data : {

{

tg_i":" 1080723 ",

contest_i" : "1001",

sid : "1567515287"

},

{

tg_id : "1080723",

contest_id : "1001",

sid : "1567515287"

}

},

total : "2"

}

然后经过修改,变成:{"data":{{"tg_i":"1080723","contest_i":"1001","sid":"1567515287"},{"tg_id":"1080723","contest_id":"1001","sid":"1567515287"}},"total":"2"}

格式化为:{

"data" : { {

"tg_i" : "1080723",

"contest_i" : "1001",

"sid" : "1567515287"

}, {

"tg_id" : "1080723",

"contest_id" : "1001",

"sid" : "1567515287"

}

},

"total" : "2"

}

但是,去用json解析,但是出错:Python 2.7.3 (default, Apr 10 2012, 23:24:47) [MSC v.1500 64 bit (AMD64)] on win32

Type "copyright", "credits" or "license()" for more information.

>>> import json

>>> json.loads(‘{"data":{{"tg_i":"1080723","contest_i":"1001","sid":"1567515287"},{"tg_id":"1080723","contest_id":"1001","sid":"1567515287"}},"total":"2"}’)

Traceback (most recent call last):

File "", line 1, in

json.loads(‘{"data":{{"tg_i":"1080723","contest_i":"1001","sid":"1567515287"},{"tg_id":"1080723","contest_id":"1001","sid":"1567515287"}},"total":"2"}’)

File "D:\tmp\dev_install_root\Python27_x64\lib\json\__init__.py", line 326, in loads

return _default_decoder.decode(s)

File "D:\tmp\dev_install_root\Python27_x64\lib\json\decoder.py", line 366, in decode

obj, end = self.raw_decode(s, idx=_w(s, 0).end())

File "D:\tmp\dev_install_root\Python27_x64\lib\json\decoder.py", line 382, in raw_decode

obj, end = self.scan_once(s, idx)

ValueError: Expecting property name: line 1 column 9 (char 9)

【折腾过程】

1.刚开始,看了看:{"data":{{"tg_i":"1080723","contest_i":"1001","sid":"1567515287"},{"tg_id":"1080723","contest_id":"1001","sid":"1567515287"}},"total":"2"}

好像也都是正常的,没什么错误。

2.后来才发现,对应着错误的位置:

column 9

是大括号'{‘

对应着,格式化的结果中的:"data" : { {

中的第一个大括号

然后才发现,原来是,data键的值,此处写成用大括号括起来的:"data" : { {

"tg_i" : "1080723",

"contest_i" : "1001",

"sid" : "1567515287"

}, {

"tg_id" : "1080723",

"contest_id" : "1001",

"sid" : "1567515287"

}

},

但是,实际上,data的值,只是两个dict,所以,应该是:

要么改为list:"data" : [{

"tg_i" : "1080723",

"contest_i" : "1001",

"sid" : "1567515287"

}, {

"tg_id" : "1080723",

"contest_id" : "1001",

"sid" : "1567515287"

}

],

要么改为dict:"data1" : {

"tg_i" : "1080723",

"contest_i" : "1001",

"sid" : "1567515287"

},

"data2" : {

"tg_id" : "1080723",

"contest_id" : "1001",

"sid" : "1567515287"

},

对应着,非格式化的内容就是:{"data":[{"tg_i":"1080723","contest_i":"1001","sid":"1567515287"},{"tg_id":"1080723","contest_id":"1001","sid":"1567515287"}],"total":"2"}

和:{"data1":{"tg_i":"1080723","contest_i":"1001","sid":"1567515287"}, "data2":{"tg_id":"1080723","contest_id":"1001","sid":"1567515287"},"total":"2"}

这样才可以正常解析:>>> json.loads(‘{"data":[{"tg_i":"1080723","contest_i":"1001","sid":"1567515287"},{"tg_id":"1080723","contest_id":"1001","sid":"1567515287"}],"total":"2"}’)

{u’total’: u’2′, u’data’: [{u’sid’: u’1567515287′, u’contest_i’: u’1001′, u’tg_i’: u’1080723′}, {u’tg_id’: u’1080723′, u’contest_id’: u’1001′, u’sid’: u’1567515287′}]}

>>> json.loads(‘{"data1":{"tg_i":"1080723","contest_i":"1001","sid":"1567515287"}, "data2":{"tg_id":"1080723","contest_id":"1001","sid":"1567515287"},"total":"2"}’)

{u’total’: u’2′, u’data1′: {u’sid’: u’1567515287′, u’contest_i’: u’1001′, u’tg_i’: u’1080723′}, u’data2′: {u’tg_id’: u’1080723′, u’contest_id’: u’1001′, u’sid’: u’1567515287′}}

【总结】

折腾json的话,前提要了解json的语法和规则。

其次才是用某种语言去处理json。

提示:

1.关于json的语法,可参考:

2.关于如何用不同语言处理json,比如Python,C#等,可参考:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值