Use Python to parse JSON and pythonjson
This section describes how to use Python to encode and decode JSON objects.
JSON (JavaScript Object Notation) is a lightweight data exchange format that is easy to read and write.
JSON Functions
To use the JSON function, you need to import the json Library:Import json.
Function |
Description |
Json. dumps |
Encodes a Python object into a JSON string. |
Json. loads |
Decodes the encoded JSON string into a Python object. |
Json. dumps
Json. dumps is used to encode a Python object into a JSON string.
Syntax
json.dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, encoding="utf-8", default=None, sort_keys=False, **kw)
Instance
The following example encodes arrays into JSON data:
#!/usr/bin/pythonimport jsondata = [ { 'a' : 1, 'b' : 2, 'c' : 3, 'd' : 4, 'e' : 5 } ]json = json.dumps(data)print json
The code execution result is:
[{"a": 1, "c": 3, "b": 2, "e": 5, "d": 4}]
Use parameters to format the output of JSON data:
>>> import json>>> print json.dumps({'a': 'Runoob', 'b': 7}, sort_keys=True, indent=4, separators=(',', ': ')){ "a": "Runoob", "b": 7}
Conversion from the original python type to the json type table:
Python |
JSON |
Dict |
Object |
List, tuple |
Array |
Str, unicode |
String |
Int, long, float |
Number |
True |
True |
False |
False |
None |
Null |
Json. loads
Json. loads is used to decode JSON data. This function returns the Data Type of the Python field.
Syntax
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 jsonjsonData = '{"a":1,"b":2,"c":3,"d":4,"e":5}';text = json.loads(jsonData)print text
The code execution result is:
{u'a': 1, u'c': 3, u'b': 2, u'e': 5, u'd': 4}
Json type conversion to python type table:
JSON |
Python |
Object |
Dict |
Array |
List |
String |
Unicode |
Number (int) |
Int, long |
Number (real) |
Float |
True |
True |
False |
False |
Null |
None |
Use a third-party Library: Demjson
Demjson is a third-party python module library that can be used to encode and decode JSON data. It contains the formatting and validation functions of JSONLint.
Github address: https://github.com/dmeranda/demjson
Official Address: http://deron.meranda.us/python/demjson/
Environment Configuration
Before using Demjson to encode or decode JSON data, we need to install the Demjson module. In this tutorial, We will download Demjson and install:
$ tar -xvzf demjson-2.2.3.tar.gz$ cd demjson-2.2.3$ python setup.py install
More Installation Introduction view: http://deron.meranda.us/python/demjson/install
JSON Functions
Function |
Description |
Encode |
Encodes a Python object into a JSON string. |
Decode |
Decodes the encoded JSON string into a Python object. |
Encode
The Python encode () function is used to encode a Python object into a JSON string.
Syntax
demjson.encode(self, obj, nest_level=0)
Instance
The following example encodes arrays into JSON data:
#!/usr/bin/pythonimport demjsondata = [ { 'a' : 1, 'b' : 2, 'c' : 3, 'd' : 4, 'e' : 5 } ]json = demjson.encode(data)print json
The code execution result is:
[{"a":1,"b":2,"c":3,"d":4,"e":5}]
Decode
Python can use the demjson. decode () function to decode JSON data. This function returns the Data Type of the Python field.
Syntax
demjson.decode(self, txt)
Instance
The following example shows how Python decodes a JSON object:
#!/usr/bin/pythonimport demjsonjson = '{"a":1,"b":2,"c":3,"d":4,"e":5}';text = demjson.decode(json)print text
The code execution result is:
{u'a': 1, u'c': 3, u'b': 2, u'e': 5, u'd': 4}