The json
module already implements some basic pretty printing with the indent
parameter:
(json
模块已经使用indent
参数实现了一些基本的漂亮打印:)
>>> import json
>>>
>>> your_json = '["foo", {"bar":["baz", null, 1.0, 2]}]'
>>> parsed = json.loads(your_json)
>>> print(json.dumps(parsed, indent=4, sort_keys=True))
[
"foo",
{
"bar": [
"baz",
null,
1.0,
2
]
}
]
To parse a file, use json.load()
:
(要解析文件,请使用json.load()
:)
with open('filename.txt', 'r') as handle:
parsed = json.load(handle)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…