[Tips] python json解析

将字符串内容解析为python对象,和将python对象转换为字符串内容,使用:

json.loads(text字符串)
json.dumps(python对象)
# python3
import json

data={}
json_str = json.dumps(data)
print ("Python 原始数据:", repr(data1))
print ("JSON 对象:", json_str)


data1 = json.loads(json_str)

  

如果是处理文件,使用:

json.load(文件句柄)
json.dump(python对象,文件句柄)

with open('data.json', 'w') as f:
json.dump(data, f)

with open('data.json', 'r') as f:
data = json.load(f)

  

原文地址:https://www.cnblogs.com/immortalBlog/p/11510788.html