跳转至

🔥AI副业赚钱星球

点击下面图片查看

郭震AI

title: a Python object (dict): date: 2024-11-28


@author jackzhenguo
@desc
@tag
@version 
@date 2020/04/01

194 python对象转json对象

import json
python_obj = {
  "name": "David",
  "class":"I",
  "age": 6  
}
print(type(python_obj))

使用json.dumps方法转化为json对象:

j_data = json.dumps(python_obj)

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))
京ICP备20031037号-1