Home > Backend Development > Python Tutorial > Detailed explanation of Python's parsing of JSON

Detailed explanation of Python's parsing of JSON

巴扎黑
Release: 2017-09-19 11:13:55
Original
2102 people have browsed it

JSON function

Using the JSON function requires importing the json library: import json.

Function Description

json.dumps Encode Python objects into JSON strings

json.loads Decode encoded JSON strings into Python objects

json.dumps

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)

Example

The following example encodes the array into JSON format data:

#!/usr/bin/python
import json
data = {'number': 6, 'name': 'Pythontab'}
jsonData = json.dumps(data)
print jsonData
Copy after login

The execution result of the above code is:

{"number": 6, "name": "Pythontab"}
Copy after login

Note: You may find that after executing the above conversion, the data has not changed. Here is what I want to say: In json, double quotes are the marked strings. Separation symbols, single quotes are not standard.

Use parameters to sort JSON data and format the output:

#!/usr/bin/python
import json
data = {'number': 6, 'name': 'Pythontab'}
jsonData = json.dumps(data, sort_keys=True, indent=4, separators=(',', ': '))
print jsonData
Copy after login

Output results

{
    "name": "Pythontab",
    "number": 6
}
Copy after login

Conversion table from python original type to json type:

##dictobjectlist, tuplearraystr, unicodestringint, long, floatnumberTruetrueFalsefalseNonenull
PythonJSON
##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]]]]]] ]])

Examples

The following examples show how Python decodes JSON objects:

#!/usr/bin/python
import json
jsonData = '{"number": 6, "name": "Pythontab"}'
str = json.loads(jsonData)
print str
Copy after login

The execution result of the above code is:

{u'number': 6, u'name': u'Pythontab'}
Copy after login

json type conversion to Python type comparison table:

##JSONPythonobjectdictarrayliststring unicodenumber (int)int, longnumber (real)floattrueTrueFalseNoneUse third-party library: Demjson
##false
null
Demjson is the first version of python A third-party module library that can be used to encode and decode JSON data, including the formatting and verification functions of JSONLint.


Github address: https://github.com/dmeranda/demjson

Environment configuration

Before using Demjson to encode or decode JSON data, we need to install the Demjson module first .

Method 1: Source code installation

$ tar -xvzf demjson-2.2.4.tar.gz

$ cd demjson-2.2.4

$ python setup.py install

Method 2: Use pip to install directly

pip install Demjson

JSON function

Function Description

encode Encode Python objects into JSON strings

decode You can use the demjson.decode() function to decode JSON data. This function returns the data type of the Python field.

encode syntax

demjson.encode(self, obj, nest_level=0)

decode syntax

demjson.decode(self, txt)

It’s very simple to use, so I won’t give examples here~~

The above is the detailed content of Detailed explanation of Python's parsing of JSON. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template