本文目录一览:
- 1、请教一个关于python处理json的问题
- 2、python调用json后关闭程序会清空json文件怎么改?
- 3、python如何修改JSON文件中的某个key值?
- 4、Python如何修改JSON里的内容?
请教一个关于python处理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。
python调用json后关闭程序会清空json文件怎么改?
一、代码不用百修改:
二、user_info.json 文件的内容需要度修改。问
修改有多种方式,需要根据你的实际答需求内来确定,以下提供两种参容考:
1、单一 json 数组
["yuri", "yuki", "yuri"]
2、键值对数组
{
"name": ["yuri", "yuki", "yuri"]
}
三、实际运行验证
python如何修改JSON文件中的某个key值?
with open('test.json', 'r') as fr: # 文件路径自己改成完整的
json_all = json.load(fr)
json_all['data1']['xxx'] = "yyy"
json_all['data2']['aaa'] = "bbb"
with open(file, 'w+') as fw:
json.dump(json_all, fw, ensure_ascii=False, indent=4)
Python如何修改JSON里的内容?
可以首先使用json包的loads函数对json数据进行解析,然后就可以像操作Python数据格式一样对数据进行索引和遍历了。
import json
s = '{"aescCityList":null,"cityAllList":null,"cityJsonArray"...'
data = json.loads(s)
for city in data["cityJsonArray"]:
if city["cityId"] == 4:
print city