了解 json
JSON介绍
JSON官方说明
Python操作json的标准api库
练习 json
#!/usr/bin/env python
#coding:utf-8
"""
FuncName: json.py
Desc: study json for python 2.7
Date: 2016-09-06 10:30
Author: johnny
"""
import json
data = {
'name' : 'Johnny',
'home' : 'http://blog.csdn.net/z_johnny',
'date' : 20160906
}
json_str = json.dumps(data) # 字符串编码JSON数据,<type 'str'>
print type(json_str)
print repr(data),type(repr(data)) # 原始输出,repr(data),<type 'str'>
python_dict = json.loads(json_str) # 字符串解码JSON数据,<type 'dict'>
print type(python_dict)
with open('jsonfile.json', 'w') as f: # 文件编码JSON数据,写JSON数据
json.dump(data, f)
with open('jsonfile.json', 'r') as f: # 文件解码JSON数据,读JSON数据,<type 'dict'>
data_dict = json.load(f)
print type(data_dict)
#带参数,example:
ex = json.dumps(data,sort_keys=True,indent=4) # sort_keys 排序,indent 缩进
print ex
"""
{
"date": 20160906,
"home": "http://blog.csdn.net/z_johnny",
"name": "Johnny"
}
"""
python原始类型向json类型的转化表:
json类型到python类型转化表: