Use of Python JSON modules

Dumps () converts Python data to JSON format

Transformation mapping table

Python JSON
dict object
list,tuple array
str,unicode string
int,float,long number
True true
False false
None null
import json
li={'a':1.'c':3.'b':2}
print(json.dumps(li))
print(type(json.dumps(li)))
Copy the code

Set indent to indent

import json
li={'a':1.'c':3.'b':2}
print(json.dumps(li,indent=2))
print(type(json.dumps(li)))
Copy the code

Sorting sort_keys

import json
li={'a':1.'c':3.'b':2}
print(json.dumps(li,sort_keys=True,indent=2))
print(type(json.dumps(li)))
Copy the code

Loads () convert JSON data into Python data

Transformation mapping table

JSON Python
object dict
array list
string unicode
number(int) int,long
number(real) float
trun Trun
false False
null None
import json
li={'data': {'a':1.'c':3.'b':2,}}
a=json.dumps(li)
print(json.loads(a))
print(type(json.loads(a)))
Copy the code