In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to understand the encoding and decoding method of Python3 built-in json module". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to understand the encoding and decoding methods of Python3's built-in json module".
Catalogue
Introduction to JSON
Dumps coding
Coding dictionary
Coding list
Encoding string
Formatted output JSON
Conversion relation comparison table
Loads decoding
Summary
Introduction to JSON
JSON (JavaScript Object Notation) is a lightweight data exchange format based on a subset of ECMAScript. JSON adopts a completely language-independent text format, which makes JSON an ideal data exchange format, easy for people to read and write, and easy for machine parsing and generation, so it is not often used in interface data development and transmission.
In Python3, we use the built-in module json to decode and encode JSON objects. The json module provides four functions: dumps, dump, loads, and load
Dumps converts data types to strings
Dump converts the data type to a string and stores it in a file
Loads converts strings to data types
Load converts file opening from a string to a data type
Dumps coding
We use dumps to encode Python objects into JSON objects. Of course, dumps only serializes to str, while dump must pass a file descriptor to save the serialized str to the file.
Coding dictionary import json odata = {'www': 1,' pythontab.com': 2, 'Python3': 3} jdata = json.dumps (odata) print (jdata)
Example result:
{"www": 1, "pythontab.com": 2, "Python3": 3} Encoding list import json ldata = [100,' Python2', {'www': 1,' pythontab.com': 2, 'Python3': 3}] jdata = json.dumps (ldata) print (jdata)
Example result:
[100,100 "Python3", {"www": 1, "pythontab.com": 2, "Python3": 3}] Encoding string import json sdata = 'Python3'jdata = json.dumps (sdata) print (jdata)
Example result:
"Python3" formatted output JSON
Convert the following array to the standard json format
Import json ldata = ['Python3', 100,{' www': 1, 'pythontab.com': 2,' Python3': 3}, True] jdata = json.dumps (ldata, sort_keys=True, indent=4) print (jdata)
Example result:
["Python3", 100,100,{ "Python3": 3, "pythontab.com": 2, "www": 1}, true]
Parameter resolution:
Sort_keys=True, then the output of the dictionary is sorted in the order of keys.
Indent=4 represents an indentation of 4, and if indent is a non-negative integer or string, then JSON array elements and object members are beautified to the indentation level specified by that value.
Conversion relation comparison table
The following is a comparison table of the conversion of Python primitive types to JSON objects:
PythonJSONdictobjectlist, tuplearraystr, unicodestringint, long, floatnumberTruetrueFalsefalseNonenullloads decoding
We use loads to decode JSON objects. The decoding result is the corresponding Python object type. Of course, loads only completes deserialization, load only receives file descriptors, reads files and deserializes them.
For example, we use it to decode the data from the previous example.
Import json jsondata =''["Python3", 100,100,{ "Python3": 3, "pythontab.com": 2, "www": 1}, true] 'ldata = json.loads (jsondata) print (type (ldata)) print (ldata)
Example result:
['Python3', 100,{' Python3': 3, 'pythontab.com': 2,' www': 1}, True]
You can see that we have successfully decoded the JSON object in the previous example, and the final decoding result is the Python list object type, which is consistent with the result of the Python object JSON object comparison table.
At this point, I believe you have a deeper understanding of "how to understand the encoding and decoding methods of Python3's built-in json module". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.