I. Conceptual understanding
1, Json.dumps () and json.loads () are JSON format processing functions (so to understand, JSON is a string)
(1) the Json.dumps () function is an encoding of a Python data type list in JSON format (so to understand that the json.dumps () function converts a dictionary to a string)
(2) the Json.loads () function converts the JSON format data to a dictionary (as you can understand, the json.loads () function converts a string into a dictionary)
2, Json.dump () and Json.load () are mainly used to read and write JSON file functions
Second, the Code test
1.py
1 ImportJSON2 3 #use of the Json.dumps () function to convert the dictionary to a string4Dict1 = {" Age":" A"}5Json_info =json.dumps (Dict1)6 Print("type of Dict1:"+str (type (DICT1)))7 Print("handled by the Json.dumps () function:")8 Print("type of Json_info:"+str (Type (json_info)))
Run:
2.py
1 ImportJSON2 3 #use of the Json.loads function to convert a string into a dictionary4Json_info ='{"Age": " " "}'5Dict1 =json.loads (json_info)6 Print("type of Json_info:"+str (type (json_info)))7 Print("handled by the Json.dumps () function:")8 Print("type of Dict1:"+str (Type (DICT1)))
Run:
3.py
1 import JSON 2 3 # Json.dump () function, write JSON information into file 4 Json_info = " {' age ': ' '"} " 5 file = open (" Span style= "COLOR: #800000" >1.json ", w ", encoding= " utf-8 " ) 6 json.dump (json_info,file)
Run (1.json file):
4.py
1 Import JSON 2 3 # The use of the Json.load () function will read the JSON information 4 File = open ('1.json','r', encoding=' utf-8')5 info = json.load (file)6 Print(info)
Run:
The difference between the four functions Json.dumps () and Json.loads (), Json.dump (), and Json.load () involved in JSON file processing in Python