JSON and Pickle
Two modules for serialization
JSON, used to convert between string and Python data types
Pickle for conversion between Python-specific types and Python data types
The JSON module provides four functions: dumps, dump, loads, load
The Pickle module provides four functions: dumps, dump, loads, load
JSON dumps convert data types to strings
Dump converts the data type into a string and stores it in a file
Loads converting a string to a data type
Load converts a file from a string to a data type
You can see that the direct operation of the file is not with "s" Dump and load, directly to the memory operation is the "s".
Pickle
Now there is a scene in the exchange of data between different devices very low way is to transfer files, dumps can directly send the memory of server A to other servers, such as B server, in many scenarios if you use pickle, that A and B programs are Python program This is not realistic , a lot of the time is different between the memory exchange between the program what to do? Using JSON and JSON can dump the results more readable, then someone asked, that still use pickle do not directly useJSON, so JSON can only serialize common data types (lists, dictionaries, lists, strings, numbers), like date formats, class object JSON, and only use pickle. Why can't he serialize the above stuff? Because JSON is cross-lingual!
Let's look at a small example of JSON:
import requests
import json
response = requests.get(‘http://wthrcdn.etouch.cn/weather_mini?city=北京‘)
response.encoding = ‘utf-8‘
dic = json.loads(response.text)
print(type(dic))
print(dic)
print(dic[‘data‘][‘city‘])
Output: This is the processing of data in memory, we are looking at the processing of a data in a file: The storage format of the data within the file:
[
{ "title" : [ ", "reply" Span class= "pun" >: [ "4" "author" : [ "zzy64421"
{ "title" : [ ", "reply" : [ "1" "author" : [ "\U6709\U70B9\U5C0F\U538C\U4E16" Span class= "pun")},
{ "title" : [ " , "reply" : [ "7" "author" : Span class= "pun" >[ "\u94c1\u8840\u591c\u5e1d" ]},
{ "title" : [ ", "reply" : [ "4" ], Span class= "PLN" > "author" : Span class= "pun" >[ "C\u8c6ay" ]},
{ "title" : [ ", "reply" : Span class= "pun" >[ "$" ], "author" : [ "\u5c0f\u50bb\u86cb\u5a03\u5a03" ]},
{ "title" : [ ", "reply" : [ "0" ], Span class= "PLN" > "author" : Span class= "pun" >[ "\u5de6\u4ed3\u871c\u67d1" ]},
{ "title" : [ ", "reply" Span class= "pun" >: [ "Wuyi" "author" : [ "fhg1225"
{ "title" : [ ", "reply" : Span class= "pun" >[ "9" ], "author" : [ "200901491" ]},
{"title": ["\u6709\u722c\u866b\u8f6f\u4ef6\u53ef\u4ee5\u91c7\u96c6\u5fae\u535a\u3001\u8d34\u5427\u3001\u77e5\u4e4e\u7684\u4e48\uff1f"], "reply": ["3"], "author": ["\u738b\u9053\u653b\u7565"]}
]
Overall, this is a list, and each element in the list is a dictionary.
import json
f = open(‘items.json‘)
a = json.load(f)
print(type(a))
print(a[0])
print(a[0][‘author‘])
f.close()
Output Result:
With these two examples we see the use of JSON and the difference between load and loads.
Note: Double quotes must be used inside the string, such as"Reply": ["3"], cannot be written as: 'reply ': ['3 ']. because JSON is cross-lingual, other languages use double quotes to denote strings, so be aware of this in Python
Null
JSON and Pickle