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

【问题】

别人给了一个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 "<pyshell#2>", line 1, in <module>

    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)

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 loads ok for updated json string

【总结】

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

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

 

提示:

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

【整理】什么是JSON+如何处理JSON字符串

 

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

【示例】Python中处理json的示例代码

【已解决】C#中,Json字符串转字典,并获得对应的Dictionary的键值



One Thought on “【已解决】python中json.loads出错:ValueError: Expecting property name: line 1 column 9 (char 9)

  1. Pingback: 请教一个关于python处理json的问题 - Python - 开发者第2226594个问答

发表评论

电子邮件地址不会被公开。 必填项已用*标注

无觅相关文章插件,快速提升流量