python中json的序列化,反序列化分别对应encoding,decoding
encoding:把一个python对象编码转换为json串
decoding:把json串转码成python对象
json字符串:
import json
test = [{"name":"('guo','jing')","address","changsha"}]
str1 = json.dumps(test,sort_keys=True,indent=2,
separators
=
(
','
,
': '
))
)
sort_keys 是是否按键值排序,indent是缩进控制json格式,separators 也是控制格式的转换字符
结果为:
[
{
"address": "changsha",
"name": "('guo','jing')"
}
]
str2 = json.loads(str1)
str2-->[{'name': "('qing','quanzhong')", 'address': 'changsha'}]
结果类型是list
for i in range(len(c)):
for key,value in c[i].items():
print(key,value)
或者
for _,item in enumerate(c):
for key,vlalue in item.items():
print(key,value)
结果:
name ('guo,'jing')
address changsha
josn文件:
import json
a)jsonText = json.load(open("weather.json"))
b)josonText = json.JSONDecode.decode(
open("weather.json").read()
)
type(jsonText)--->dict 可以通过遍历字典来实现获得值
结果如下: