JSON (JavaScript Object Notation) is a lightweight data interchange format. It is based on a subset of ECMAScript.
The JSON has these forms:
An object is an unordered collection of "name/value pairs". An object starts with "{" (opening parenthesis) and "}" (the closing parenthesis) ends. Each "name" is followed by a ":" (colon); "' Name/value ' pair ' is separated by", "(comma).
The JSON module can be used to encode JSON data in Python3, which contains two functions:
- json.dumps (): encodes the data.
- json.loads (): decodes the data.
In the JSON encoding and decoding process, the original Python type and JSON type will be converted to each other, the specific conversions against the following:
Python encoding is a JSON type conversion table:
Python |
JSON |
Dict |
Object |
List, tuple |
Array |
Str |
String |
int, float, int-& float-derived Enums |
Number |
True |
True |
False |
False |
None |
Null |
JSON decodes the corresponding table for the Python type conversion:
JSON |
Python |
Object |
Dict |
Array |
List |
String |
Str |
Number (int) |
Int |
Number (real) |
Float |
True |
True |
False |
False |
Null |
None |
Json.dumps and Json.loads instances
The following example demonstrates the conversion of a PYTHON data structure to JSON:
1 #!/usr/bin/python32 3 ImportJSON4 5 #Python dictionary type converted to JSON object6data = {7 'No': 1,8 'name':'Runoob',9 'URL':'http://www.runoob.com'Ten } One AJson_str =json.dumps (data) - Print("Python raw Data:", repr (data)) - Print("JSON object:", JSON_STR)
Execute the above code to output the result:
Python Raw data: {‘Url: ' Http://www.runoob.com ', ' no ' : 1, ' name ' : ' Runoob ' }json object: {< span class= "str" > "url" : "HTTP +/" Www.runoob.com ", " no ": 1, "name" : }
The result of the output shows that the simple type is very similar by encoding followed by its original repr () output.
Next, we can convert a JSON-encoded string back to a python data structure:
1 #!/usr/bin/python32 3 ImportJSON4 5 #Python dictionary type converted to JSON object6Data1 = {7 'No': 1,8 'name':'Runoob',9 'URL':'http://www.runoob.com'Ten } One AJson_str =json.dumps (data1) - Print("Python raw Data:", repr (data1)) - Print("JSON object:", Json_str) the - #Convert a JSON object to a Python dictionary -Data2 =json.loads (JSON_STR) - Print("data2[' name ']:", data2['name']) + Print("data2[' url ']:", data2['URL'])
Execute the above code to output the result:
Python Raw data: {‘Name: ' Runoob ', ' No ': 1, ' URL ': ' Http://www.runoob.com '}Json { "name" : "Runoob" , "No" : 1, "url" : "http://www.runoob.com" }data2[ ' name ' ]: runoobdata2[ ' URL ' ]: Http://www.runoob.com
If you are dealing with files instead of strings, you can use json.dump () and json.load () to encode and decode JSON data. For example:
1 #Writing JSON Data2With open ('Data.json','W') as F:3 json.dump (data, F)4 5 #reading Data6With open ('Data.json','R') as F:7data = Json.load (f)
*
Copy from: /HTTP/ www.runoob.com/python3/python3-json.html
The more refer to:https://docs.python.org/3/ library/json.html
What's JSON