current position:Home>Use of Python JSON module

Use of Python JSON module

2022-02-02 04:54:53 Xiao Wang is not serious

Python JSON Use of modules

dumps( ) take Python Data transformation JSON Format

Conversion correspondence 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 code 

image-20211211111328623

Set indent indent

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

image-20211211111540051

Sort 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 code 

image-20211211111938447

loads( ) take JSON Format data into Python data

Conversion correspondence 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 code 

image-20211211121610272

copyright notice
author[Xiao Wang is not serious],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/02/202202020454527812.html

Random recommended