@author jackzhenguo
@desc
@tag
@version
@date 2020/04/01
import json
# a Python object (dict):
python_obj = {
"name": "David",
"class":"I",
"age": 6
}
print(type(python_obj))
使用json.dumps
方法转化为json对象:
# convert into JSON:
j_data = json.dumps(python_obj)
# result is a JSON string:
print(j_data)
若字典转化为json对象后,保证键有序,且缩进4格,如何做到?
json.dumps(j_str, sort_keys=True, indent=4)
例子:
import json
j_str = {'4': 5, '6': 7, '1': 3, '2': 4}
print(json.dumps(j_str, sort_keys=True, indent=4))