python操作json数据格式--基础

非常基础的json库的用法,后续添加数据格式、编码等内容

参考文章

json进阶

Python的json模块提供了一种很简单的方式来编码和解码JSON数据。 其中两个主要的函数是 json.dumps() 和 json.loads() , 要比其他序列化函数库如pickle的接口少得多。 下面演示如何将一个Python数据结构转换为JSON:

1
2
3
4
5
6
7
8
9
import json
 
data = {
'name' : 'ACME',
'shares' : 100,
'price' : 542.23
}
 
json_str = json.dumps(data)


下面演示如何将一个JSON编码的字符串转换回一个Python数据结构:

1
data = json.loads(json_str)


如果你要处理的是文件而不是字符串,你可以使用 json.dump() 和 json.load() 来编码和解码JSON数据。例如:

1
2
3
4
5
6
7
# Writing JSON data
with open('data.json', 'w') as f:
 json.dump(data, f)
 
# Reading data back
with open('data.json', 'r') as f:
 data = json.load(f)
 
posted @ 2017-04-05 17:20  dahu1  Views(347)  Comments(0Edit  收藏  举报