Transferred from: https://www.cnblogs.com/wangyayun/p/6699184.html?utm_source=tuicool&utm_medium=referral
Shows you how to encode and decode JSON objects using the Python language.
JSON (JavaScript Object Notation) is a lightweight data interchange format that is easy for people to read and write.
JSON functions
Use JSON functions to import the JSON library: importJSON.
function |
Description |
Json.dumps |
Encode a Python object as a JSON string |
Json.loads |
Decodes an encoded JSON string into a Python object |
Json.dumps
The json.dumps is used to encode a Python object into a JSON string.
Grammar
Json.Dumps(Obj,Skipkeys=False,Ensure_ascii=True,Check_circular=true, Allow_nan=true< Span class= "pun" >, Cls=none Indent=none, Separators=none, encoding = "utf-8" , default< Span class= "pun" >=none, Sort_keys=false, **kw /span>
Instance
The following example encodes an array into JSON-formatted data:
#!/usr/bin/pythonImportJsondata= [ { A : 1, ' B ' : 2, ' C ' : 3, : 4 ' E ' : 5 } ]json = Json. (data) print< Span class= "PLN" > json
The result of the above code execution is:
[{"a":1,"C":3,"B":2,"E":5,"D" :4}]
Use parameters to format the output of JSON data:
>>> ImportJson>>> PrintJson.Dumps({A: ' Runoob ', ' B ' : 7 }, Sort_keys=true,< Span class= "PLN" > Indent=4, separators< Span class= "pun" >= ( ', ' , ': ' { "a" : "Runoob" , "B" : 7}
Python primitive type conversion table to JSON type:
Python |
JSON |
Dict |
Object |
List, tuple |
Array |
STR, Unicode |
String |
int, long, float |
Number |
True |
True |
False |
False |
None |
Null |
Json.loads
The json.loads is used to decode JSON data. The function returns the data type of the Python field.
Grammar
JSON. Loads(s[, encoding[, cls[, object_hook[, parse_float[, Parse_ int[, parse_constant[, object_pairs_hook[,* *kw] ]] []]
Instance
The following example shows how Python decodes a JSON object:
#!/usr/bin/pythonImport=' {' A ': 1, ' B ': 2, ' C ': 3, ' d ': 4, ' E ': 5} '; = json. Loads(jsondata)print text
The result of the above code execution is:
{u' A ':1, u 'C ':3, u 'B ':2, u' E ': 5, u' d ':4}
JSON type conversion to Python type comparison table:
JSON |
Python |
Object |
Dict |
Array |
List |
String |
Unicode |
Number (int) |
int, long |
Number (real) |
Float |
True |
True |
False |
False |
Null |
None |
Using third-party libraries: Demjson
Demjson is a third-party module library of Python that can be used to encode and decode JSON data, including the Jsonlint formatting and validation functions.
Github Address: Https://github.com/dmeranda/demjson
Official address: http://deron.meranda.us/python/demjson/
Environment configuration
Before using Demjson encoding or decoding JSON data, we need to install the Demjson module first. In this tutorial we will download Demjson and install:
-xvzf Demjson-2.2. 3.tar. gz$ CD Demjson-2.2. 3$ python setup. PY Install
More installation Information view: Http://deron.meranda.us/python/demjson/install
JSON functions
function |
Description |
Encode |
Encode a Python object as a JSON string |
Decode |
Decodes an encoded JSON string into a Python object |
Encode
The Python encode () function encodes a Python object into a JSON string.
Grammar
Demjson. Encode(self, obj, nest_level=0)
Instance
The following example encodes an array into JSON-formatted data:
#!/usr/bin/pythonImportDemjsondata= [ { A : 1, ' B ' : 2, ' C ' : 3, : 4 ' E ' : 5 } ]json = Demjson. (data) print< Span class= "PLN" > json
The result of the above code execution is:
[{"a":1,"B":2,"C":3,"D":4,"E" :5}]
Decode
Python can use the Demjson.decode () function to decode JSON data. The function returns the data type of the Python field.
Grammar
Demjson. Decode(self, txt)
Instance
The following example shows how Python decodes a JSON object:
#!/usr/bin/pythonImport=' {' A ': 1, ' B ': 2, ' C ': 3, ' d ': 4, ' E ': 5} '; = Demjson. Decode(json)print text
The result of the above code execution is:
{u' A ':1, u 'C ':3, u 'B ':2, u' E ': 5, u' d ':4}
Python JSON uses