【问题】
别人给了一个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 >>> 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): 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"}’) >>> 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#等,可参考:
【已解决】C#中,Json字符串转字典,并获得对应的Dictionary的键值
转载请注明:在路上 » 【已解决】python中json.loads出错:ValueError: Expecting property name: line 1 column 9 (char 9)