Serialization and de-serialization of Python JSON format learning

View json library method

Import   JSON
 Print ( " main method JSON library: " , JSON. __all__ is )
 # primary method ## JSON library: [ 'dump', 'dumps ', 'load', 'loads', 'JSONDecoder', 'JSONDecodeError' , 'JSONEncoder'] 

dump dump and dumps the difference between the object is serialized and saved to a file            dumps object serialization is the load and the load difference between the loads serialized string read from the file and deserializing            loads serialized string deserialization

 

Python dictionary type to a JSON object

Import JSON
 # the Python dictionaries objects into a JSON 
Data = {
     ' NO ' : 001 ,
     ' name ' : ' Runoob ' ,
     ' URL ' : ' http://www.runoob.com ' ,
     ' address ' : ' Beijing ' 
} 
json_str = json.dumps (data) 
json_str1 = json.dumps (data, ensure_ascii = False )
 Print ( " primitive data types: ", type (Data))
 Print ( " the Python raw data: " , the repr (Data))   # the repr () function for the object into a form read by the interpreter. repr (object) Returns the string format object 
Print ( ' type JSON object: ' , type (json_str), type (json_str1))
 Print ( " JSON object: " , json_str)
 Print ( " JSON object character encoding ,, " , json_str1) 

'' ' 
the issue of Chinese character encoding 
when json.dumps serialize the Chinese want to use the default output encoding ascii real Chinese need to specify = False ensure_ascii:. 
Import json 
Print (json.dumps ( "China")) 
" \ u4E2D \ u56fd " 
Print (json.dumps (" China '

'''

Conversion Python dictionaries as a JSON object after re-converted to a Python type

Deserialized, decoded string python json format for data objects, and outputs the code implemented see:
 Import   json 
dict1 = { ' name ' : ' Lucky ' , ' Age ' : 27, ' address ' : ' Shenzhen ' }
 Print ( 'primitive data types: ' , type (dict1))
 Print ( ' raw data: ' , dict1)
 # of dict1 serializing process 
str1 = json.dumps (dict1, ensure_ascii = False)   # remove distortion character display 
Print ( " data type is serialized: ', type (str1))
 Print ( ' data is serialized: ' , str1,) 

# of str1 deserialized 
dict2 = json.loads (str1)
 Print ( ' data type after deserialization: ' , type (dict2))
 Print ( ' deserialized data: ' , dict2)
# The Python JSON object type to dictionary 
DATAl = {
     ' NO ' : 001 ,
     ' name ' : ' Runoob ' ,
     ' URL ' : ' http://www.runoob.com ' ,
     ' address ' : ' Beijing ' 
} 
json_str json.dumps = (DATAl, ensure_ascii = False)
 Print ( " the Python raw data: " , the repr (DATAl))
 Print ( " the JSON Object: ", Json_str) 

# convert JSON Python dictionary object 
DATA2 = json.loads (json_str)
 Print ( " DATA2 of [ 'name']: " , DATA2 [ ' name ' ])
 Print ( " DATA2 link [ 'url']: " , DATA2 [ ' URL ' ])
 Print (DATA2)

 

# If you want to deal with is the file instead of a string, you can use json.dump () and json.load () to encode and decode JSON data. For example: 
Import   JSON 
Data = {
     ' NO ' : 001 ,
     ' name ' : ' Runoob ' ,
     ' URL ' : ' http://www.runoob.com ' ,
     ' address ' : ' Beijing ' 
} 
# write data JSON 
Open with ( ' the data.json ' , ' W ' ) AS F: 
    The json.dump (Data,

# Read data 
with Open ( ' the data.json ' , ' R & lt ' ) AS F: 
    Data = the json.load (F)
     Print (Data)

 

Binding requests library, json returned data,

# ### binding database requests, data, returned JSON, specifically code: 

Import   JSON, requests 
R & lt = requests.get ( ' http://wthrcdn.etouch.cn/weather_mini?city= Shenzhen ' )
 Print (R & lt .text, U ' data type: ' , type (r.text))
 # data deserialized operation 
DIC = json.loads (r.text)
 Print (DIC, U ' data type: ' , type (DIC ))

Guess you like

Origin www.cnblogs.com/carey9420/p/12066891.html