Python interface Test (iv)

Source: Internet
Author: User
Tags serialization

V: Python serialization and deserialization

put python the object encoding into JSON the format of the string, deserialization can be understood as: the JSON format

the string is decoded to python The data object. in Python 's standard library, the JSON library is specifically provided

Import to view the main methods of the JSON library

Import  jsonprint (json.__all__)

See the main methods of the JSON library:

[' Dump ', ' dumps ', ' load ', ' loads ', ' jsondecoder ', ' Jsonencoder ']

Define a dictionary that serializes it into JSON-formatted strings through JSON, as shown in the implemented code

Import  jsondict1={' name ': ' Leizi ', ' age ': ' Address ': ' Beijing '}print (the data type before the serialization is: ', type (DICT1)) Print (U ' Pre-serialized data: ', Dict1) #对dict1进行序列化的处理str1 =json.dumps (dict1) Print (U ' serialized data type: ', type (STR1)) Print (U ' serialized data is: ', str1)

Results

The data type before the serialization is: <class ' dict ' > pre-serialized data: {' age ': ' ' name ': ' Leizi ', ' address ': ' Beijing '} The serialized data type is: <class ' str ' > The serialized data is: {"Age": $, "name": "Leizi", "Address": "\U5317\U4EAC"}

We will then deserialize the JSON-formatted string into a Python data object, see the code and output of the implementation:

Import  jsondict1={' name ': ' Ray ', ' age ': ' Address ': ' Beijing '}print (U ' data type before serialization: ', type (DICT1)) Print (U ' pre-serialized data: ' , Dict1) #对dict1进行序列化的处理str1 =json.dumps (dict1) Print (U ' serialized data type: ', type (STR1)) Print (U ' serialized data is: ', str1) # Deserialization of str1 dict2=json.loads (str1) print (U ' deserialized data type: ', type (DICT2)) Print (U ' deserialized data: ', DICT2)

Output:

The data type before serialization is : <class ' dict ' >

Pre-serialized data : {' age ': +, ' name ': ' Ray ', ' address ': ' Beijing '}

The serialized data type is : <class ' str ' >

The serialized data is : {"Age": $, "name": "\u96f7\u5b50", "Address": "\u5317\u4eac"}

Deserialized data type : <class ' dict ' >

Deserialized data: {' age ': +, ' name ': ' Ray ', ' address ': ' Beijing '}

In conjunction with the requests library, look at the JSON data that is returned, with the following code:

Import  json,requestsr=requests.get (' http://wthrcdn.etouch.cn/weather_mini?city= Beijing ') print (r.text,u ' data type: ', Type (r.text)) #对数据进行反序列化的操作dic =json.loads (r.text) print (dic,u ' data type: ', type (DIC))

Results:

{"desc": "OK", "status": +, "data": {"Wendu": "4", "Ganmao": "There will be a strong cooling process, and strong wind, very prone to colds, please pay special attention to increase clothing warm and cold." "," forecast ": [{" Fengxiang ":" North Wind "," Fengli ":" 4-5 Levels "," High ":" Hot 6 ℃ "," type ":" Cloudy "," Low ":" Cryogenic -2℃ "," date ":" 20th Monday "},{" Fengxiang ":" South Wind "," Fengli ":" Breeze Level "," High ":" Hot 1 ℃ "," type ":" Snow "," Low ":" Cryogenic -3℃ "," Date ":" 21st Tuesday "},{" Fengxiang ":" North Wind "," Fengli ":" Level 3-4 "," High ":" 8 ℃ "," type ":" Yin "," low ":" Cold -2℃ "," Date ":" 22nd Wednesday "},{" Fengxiang ":" South Wind "," Fengli ":" Breeze "," HI " : "High temperature 8 ℃", "type": "Clear", "low": " -2℃", "date": "23rd Thursday"},{"Fengxiang": "North Wind", "Fengli": "Breeze", "High": "Hot 10 ℃", "type": "Fine", " Low ":" Lo -2℃ "," date ":" 24th Friday "}]," Yesterday ": {" FL ":" Level 4-5 "," FX ":" North Wind "," High ":" Hot 15 ℃ "," type ":" Cloudy "," Low ":" Cryogenic -2℃ "," Date ": Sunday 19th"}, "AQI": "PNs", "City": "Beijing"} Data type: <class ' str ' >{' status ': +, ' data ': {' city ': ' Beijing ', ' Yesterday ': {' FX ': ' North Wind ', ' type ': ' Cloudy ', ' Date ': ' 19th ', ' fl ': ' 4-5 ', ' High ': ' 15 ℃ ', ' low ': ' Cryogenic -2℃ '}, ' forecast ': [{' Fengxiang ': ' North Wind ', ' low ': ' Lo -2℃ ', ' fengli ': ' Level 4-5 ', ' Date ': ' 20th Monday ', ' High ': ' Hot 6 ℃

', ' type ': ' Cloudy'}, {' Fengxiang ': ' Southerly', ' low ': ' Cryogenic-3 ℃', ' fengli ': ' Breeze-Class', ' Date ': ' 21st Tuesday', ' High ': ' Hot1 ℃', ' type ': ' Snow'}, {' Fengxiang ': ' The North wind', ' low ': ' Cryogenic-2 ℃', ' Fengli ': ' Level 3-4', ' Date ': ' 22nd Wednesday' high ': ' Hot 8 ℃', ' type ': ' Yin '}, {' Fengxiang ': ' South wind ', ' low ': ' Cold -2 ℃', ' fengli ': ' Breeze-Class ' , ' d Ate ': ' 23rd Thursday ', ' High ': ' Hot 8 ℃', ' type ': ' Sunny '}, {' Fengxiang ': ' North Wind ', ' low ': ' Lo -2 ℃', ' Feng Li ': ' Breeze Level ', ' Date ': ' 24th Friday ', ' High ': ' Hot 10 ℃', ' type ': ' Clear '}], ' Ganmao ': ' will have a strong cooling process, and strong wind, Very prone to colds, please pay special attention to increase clothing warm and cold. ' , ' wendu ': ' 4 ', ' AQI ': ' PNs '}, ' desc ': ' OK '} data type : <class ' dict ' >

Python interface Test (iv)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.