JSON is typically used for data exchange between Web clients and servers, that is, 字符串类型 converting data into or converting to Python基本数据类型 Python基本数据类型 字符串类型 .
Common methods 
 
  
   
   | Method | Description | 
 
  
  
   
   | Json.loads (obj) | Serializes a string into Python's basic data type, noting single and double quotation marks | 
 
   
   | Json.dumps (obj) | Serializes the basic data type of Python into a string | 
 
   
   | Json.load (obj) | Read a string from a file and serialize it into Python's basic data type | 
 
   
   | Json.dump (obj) | Serializes the basic data type of Python into a string and writes to a file | 
 
  
Instance 
 
  
  - Serializing a string into a dictionary
Create a String variabledict_str
>>> dict_str = ‘{"k1":"v1","k2":"v2"}‘# 数据类型为str>>> type(dict_str)<class ‘str‘>
Serializing a string variable dict_str into a dictionary format
>>> import json>>> dict_json = json.loads(dict_str)
View data types and output content
>>> type(dict_json)# 数据类型被序列化成字典格式了<class ‘dict‘>>>> dict_json{‘k1‘: ‘v1‘, ‘k2‘: ‘v2‘}
 
 
  
  - Serializes a variable of a list type into a string type
Create a listjson_li
>>> json_li = [11,22,33,44] # 数据类型为list>>> type(json_li)<class ‘list‘>
 
 
  
  - To convert a string type to a python base data type
>>> import json>>> json_str = json.dumps(json_li)
View data types
# 为str>>> type(json_str)<class ‘str‘>>>> json_str‘[11, 22, 33, 44]‘
 
 
  
  - Save a dictionary as a string into a db file
# Create a dictionary of data types>>>Dic={"K1":123,"K2":456}# Output Type and content>>>Print(Type(Dic),Dic)(<Type' Dict '>,{' K2 ':456,' K1 ':123})# import JSON module>>>ImportJson# convert DIC to a string and write to the db file below the current directory, if no file is created>>>Json.Dump(Dic,Open("DB","W"))# import OS Module view>>>ImportOs# View the files under the current directory>>>Os.System("Ls-l")Total8-rw-r-- r--1 root root  may 20 23:54 db0# view the contents of the file db that, the last one. 0 is the command execution succeeded >>> span class= "n" >os. System ( "cat db" ) { "K2" : 456 "K1" :  123}0            
 
 
  
  - Read the contents of the file and convert the read string into Python's basic data type
# Read the db file below the current directory, convert the content to Python's basic data type and assign the value to result>>>result = json. Load (open ( "DB"  "R" )) # View object result data type and content >>> print ( type (resultresult )  (<type  ' dict ' >{u ' K2 ' :  456u ' K1 ' : 123})     
 Python Standard library-json