Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
50 lines (37 loc) · 779 Bytes

File metadata and controls

50 lines (37 loc) · 779 Bytes
Copy raw file
Download raw file
Outline
Edit and raw actions
@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)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.