Python Json the load, loads, dump, dumps and operations analysis

Copyright: welcome the exchange of learning, please indicate the source. https://blog.csdn.net/qq_23869697/article/details/91453494

1. The format conversion json.dumps () and json.loads ()

(1) json.dumps () will be converted to a string dictionary

d={'apple': 1, 'google': 2, 'facebook': 3}  # <class 'dict'>
str_d = json.dumps(d)  #  <class 'str'>

(2) json.loads () convert string dictionary

original_d = json.loads(str_d) # <class 'dict'>

import json
d={'apple': 1, 'google': 2, 'facebook': 3}
print(type(d))
str_d = json.dumps(d)
print(type(str_d))
original_d = json.loads(str_d)
print(type(original_d))
print(str_d)

In-Circuit Debugger
load

2. document literacy json.load () and json.dump ()

(1) json.load () from the folder read json file:

with open(filepath, ‘r’) as jl:
	json_info = json.load(jl)

(2) json.dump () to save the dictionary as json format file to your local:

d={'apple': 1, 'google': 2, 'facebook': 3}

with open(filepath, ‘w’) as jd:
	json.dump(d, jd)

Reference: https://www.cnblogs.com/xiaomingzaixian/p/7286793.html

Guess you like

Origin blog.csdn.net/qq_23869697/article/details/91453494