首先贴上官方文档
https://docs.python.org/2/library/json.html?highlight=json#module-json
导入json包
1 | # -*- coding:utf-8 -*- |
函数的使用
json.dumps
1 | dictData = {'a':1, 'b':2} |
{"a": 1, "b": 2}
####json.loads
1 | print json.loads(jsonData) # 与dumps相反,将json对象转变为字典对象返回 |
{u'a': 1, u'b': 2}
####json.dump
1 | with open('test.json', 'w') as f: |
####json.load
1 | with open('test.json') as f: |
{u'a': 1, u'b': 2}