Python:json数据读取和序列化
Lasted 2020-01-18 09:40:50
JSON(JavaScript Object Notation)是一种轻量级的数据交换语言,该语言以易于让人阅读的文字为基础,用来传输由属性值或者序列性的值组成的数据对象。
对象序列化
import json
text = json.dumps([{'name':'value'}, {'bar': ('baz', None, 1.0, 2)}])
print(text)
[{"name": "value"}, {"bar": ["baz", null, 1.0, 2]}]
格式化输出
import json
text = json.dumps([{'name':'value'}, {'bar': ('baz', None, 1.0, 2)}], indent=2)
print(text)
[
{
"name": "value"
},
{
"bar": [
"baz",
null,
1.0,
2
]
}
]
JSON解码
import json
obj = json.loads('["foo", {"bar":["baz", null, 1.0, 2]}]')
print(obj)
['foo', {'bar': ['baz', None, 1.0, 2]}]