Skip to content

Latest commit

 

History

History
50 lines (37 loc) · 779 Bytes

194.md

File metadata and controls

50 lines (37 loc) · 779 Bytes
@author jackzhenguo
@desc
@tag
@version 
@date 2020/04/01

194 python对象转json对象

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

若字典转化为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))
[上一个例子](193.md) [下一个例子](195.md)