请教一个关于python处理json的问题
1个回答
展开全部
{
data : {
{
tg_i":" 1080723 ",
contest_i" : "1001",
sid : "1567515287"
},
{
tg_id : "1080723",
contest_id : "1001",
sid : "1567515287"
}
},
total : "2"
}
然后经过修改,变成:
?
1
{"data":{{"tg_i":"1080723","contest_i":"1001","sid":"1567515287"},{"tg_id":"1080723","contest_id":"1001","sid":"1567515287"}},"total":"2"}
格式化为:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
{
"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)
【折腾过程】
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键的值,此处写成用大括号括起来的:
?
1
2
3
4
5
6
7
8
9
10
"data" : { {
"tg_i" : "1080723",
"contest_i" : "1001",
"sid" : "1567515287"
}, {
"tg_id" : "1080723",
"contest_id" : "1001",
"sid" : "1567515287"
}
},
但是,实际上,data的值,只是两个dict,所以,应该是:
要么改为list:
?
1
2
3
4
5
6
7
8
9
10
"data" : [{
"tg_i" : "1080723",
"contest_i" : "1001",
"sid" : "1567515287"
}, {
"tg_id" : "1080723",
"contest_id" : "1001",
"sid" : "1567515287"
}
],
要么改为dict:
?
1
2
3
4
5
6
7
8
9
10
"data1" : {
"tg_i" : "1080723",
"contest_i" : "1001",
"sid" : "1567515287"
},
"data2" : {
"tg_id" : "1080723",
"contest_id" : "1001",
"sid" : "1567515287"
},
对应着,非格式化的内容就是:
?
1
{"data":[{"tg_i":"1080723","contest_i":"1001","sid":"1567515287"},{"tg_id":"1080723","contest_id":"1001","sid":"1567515287"}],"total":"2"}
和:
?
1
{"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。
data : {
{
tg_i":" 1080723 ",
contest_i" : "1001",
sid : "1567515287"
},
{
tg_id : "1080723",
contest_id : "1001",
sid : "1567515287"
}
},
total : "2"
}
然后经过修改,变成:
?
1
{"data":{{"tg_i":"1080723","contest_i":"1001","sid":"1567515287"},{"tg_id":"1080723","contest_id":"1001","sid":"1567515287"}},"total":"2"}
格式化为:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
{
"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)
【折腾过程】
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键的值,此处写成用大括号括起来的:
?
1
2
3
4
5
6
7
8
9
10
"data" : { {
"tg_i" : "1080723",
"contest_i" : "1001",
"sid" : "1567515287"
}, {
"tg_id" : "1080723",
"contest_id" : "1001",
"sid" : "1567515287"
}
},
但是,实际上,data的值,只是两个dict,所以,应该是:
要么改为list:
?
1
2
3
4
5
6
7
8
9
10
"data" : [{
"tg_i" : "1080723",
"contest_i" : "1001",
"sid" : "1567515287"
}, {
"tg_id" : "1080723",
"contest_id" : "1001",
"sid" : "1567515287"
}
],
要么改为dict:
?
1
2
3
4
5
6
7
8
9
10
"data1" : {
"tg_i" : "1080723",
"contest_i" : "1001",
"sid" : "1567515287"
},
"data2" : {
"tg_id" : "1080723",
"contest_id" : "1001",
"sid" : "1567515287"
},
对应着,非格式化的内容就是:
?
1
{"data":[{"tg_i":"1080723","contest_i":"1001","sid":"1567515287"},{"tg_id":"1080723","contest_id":"1001","sid":"1567515287"}],"total":"2"}
和:
?
1
{"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。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询