Python 3 Advanced JSON knowledge summary
JSON
Why JSON
Click here to learn the following while watching the video explanation
Everyone has been shopping on Taobao, Jingdong and pinduoduo.
Online shopping may use mobile phones or computer browsers.
These Taobao, JD apps on mobile phones and website pages running in computer browsers are collectively referred to as "clients".
Why can the client present a wide range of products? These product information is obtained from Taobao and Jingdong's website server
Data exchange between client and server is required to complete various functions.
For example: obtain the list of recent transactions and submit new orders.
When these data are generated by the client or server, they are the internal data objects of the client program and the server program.
Assuming that the server programs are developed in Python language, then
The latest transaction list obtained by the server from the database may be a python list object like the following:
historyTransactions = [ { 'time' : '20170101070311', # Trading time 'amount' : '3088', # Transaction amount 'productid' : '45454455555', # Article number 'productname' : 'iphone7' # Name of goods }, { 'time' : '20170101050311', # Trading time 'amount' : '18', # Transaction amount 'productid' : '453455772955', # Article number 'productname' : 'Secret laundry detergent' # Name of goods }, ... ]
The question now is, how can we pass such a data object} existing in memory to the client?
After the client receives the data, how can it be transformed into a data object in its programming language? Because it becomes an object in the program language, it is convenient to deal with it.
This process is just like when two people chat, both parties need to convert the things in their minds into language descriptions, pass them to each other, receive each other's language descriptions, and then convert them into things in their own minds.
Usually, we turn various types of data objects of a program into byte strings representing the data object. This process is called {serialization.
The process of converting a byte string into a data object in a program is called} deserialization
We transmit information through any transmission protocol (http protocol is more commonly used at present), and all transmitted are} good byte strings.
Moreover, different client and server programs may use different languages. In order to facilitate the processing of different programming languages, the serialized format should be convenient for all languages.
What serialization format to use is an important issue.
The previous popular solution was XML. We need to develop our own code to serialize the above data objects into XML documents and transmit them.
such as
<transactinlist> <trans> <time>20170101070311</time> <amount>3088</amount> <productid>45454455555</productid> <productname>iphone7</productname> </trans> <trans> <time>20170101050311</time> <amount>18</amount> <productid>453455772955</productid> <productname>Secret laundry detergent</productname> </trans> </transactinlist>
After receiving it, the receiver will deserialize it into the data object in its program for processing.
One disadvantage of XML is that the serialization performance is relatively low, and the transformed data volume increases a lot.
The recent mainstream solution is to use: JSON format
JSON (JavaScript object notation) is a lightweight data exchange format.
It is defined in the javascript specification. It is a text format to store and represent data.
It is characterized by simplicity and clarity, which can be easily understood by people.
It is also convenient for program parsing and generation. Compared with XML, the efficiency of serialization and deserialization is much higher, and the amount of data generated is much smaller.
Any programming language can use this format. Moreover, the interpreters of many programming languages have built-in libraries, which can be easily serialized and deserialized. Including Python, Javascript, etc.
serialization and deserialization
Python has built-in json library, which can easily serialize built-in data objects into json formatted text strings.
For example, if we want to serialize the above data objects into json format strings, we can use the dumps function in the library, like this
import json historyTransactions = [ { 'time' : '20170101070311', # Trading time 'amount' : '3088', # Transaction amount 'productid' : '45454455555', # Article number 'productname' : 'iphone7' # Name of goods }, { 'time' : '20170101050311', # Trading time 'amount' : '18', # Transaction amount 'productid' : '453455772955', # Article number 'productname' : 'Secret laundry detergent' # Name of goods } ] # The dumps method serializes the data object into a json formatted string jsonstr = json.dumps(historyTransactions) print(jsonstr)
The printed result is like this
[{"time": "20170101070311", "amount": "3088", "productid": "45454455555", "productname": "iphone7"}, {"time": "20170101050311", "amount": "18", "productid": "453455772955", "productname": "\u5965\u5999\u6d17\u8863\u6db2"}]
You can find that the json format represents data very much like the Python language itself.
Of course, there are some differences. For example, the string can only use double quotation marks, and there can be no comma after the last element of the list.
The result of serialization is also a!!! character string. The json format itself is a string.
Then we can store it in a file or send it out from the network.
This completes the sending of the data object.
Some friends may have found the data in the Chinese part
Secret laundry detergent
After json transformation, it becomes
\u5965\u5999\u6d17\u8863\u6db2
This is because, JSON The dumps method finds that if there is a non ascii character in the string, such as Chinese, it is represented by the unicode number of the character by default.
For example, the unicode of "Ao" is 5965, which means \ u5965
If you don't want to, you can give the parameter ensure_ascii is assigned False, as shown below
json.dumps(historyTransactions,ensure_ascii=False,indent=4)
The indent parameter indicates that the indent is 4 after conversion, so it looks neat and good-looking,
In this way, after conversion, the operation results are as follows
[ { "time": "20170101070311", "amount": "3088", "productid": "45454455555", "productname": "iphone7" }, { "time": "20170101050311", "amount": "18", "productid": "453455772955", "productname": "Secret laundry detergent" } ]
If the receiver is also developed in Python, you can use the loads method in the json library to change the string in json format into a data object in Python
such as
import json jsonstr = '[{"time": "20170101070311", "amount": "3088", "productid": "45454455555", "productname": "iphone7"}, {"time": "20170101050311", "amount": "18", "productid": "453455772955", "productname": "\u5965\u5999\u6d17\u8863\u6db2"}]' translist = json.loads(jsonstr) print(translist) print(type(translist))
The output results are as follows
[{'time': '20170101070311', 'amount': '3088', 'productid': '45454455555', 'productname': 'iphone7'}, {'time': '20170101050311', 'amount': '18', 'productid': '453455772955', 'productname': 'Secret laundry detergent'}] <class 'list'>
As you can see, it does become a list object.
In this way, the receiver program can easily process the data inside.