Details about JSON parsing in Python

Source: Internet
Author: User
The JSON function needs to be imported into the JSON Library importjson. Function description json. dumps: encodes a Python object into a JSON string json. loads... JSON function

To use the JSON function, you must 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

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 = {'number': 6, 'name': 'Pythontab'}jsonData = json.dumps(data)print jsonData

The code execution result is:

{"number": 6, "name": "Pythontab"}

Note: You may find that the data has not changed after the preceding conversion. here we will talk about the following: double quotation marks are the string separator marked in json. single quotation marks are not standard.

Sort JSON data and format the output using parameters:

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

Output result

{    "name": "Pythontab",    "number": 6}

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 = '{"number": 6, "name": "Pythontab"}'str = json.loads(jsonData)print str

The code execution result is:

{u'number': 6, u'name': u'Pythontab'}

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

Environment configuration

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

Method 1: install with source code

$ Tar-xvzf demjson-2.2.4.tar.gz

$ Demjson-2.2.4 cd

$ Python setup. py install

Method 2: install using pip directly

Pip install Demjson

JSON Functions

Function description

Encode encodes a Python object into a JSON string

Decode 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 is very easy to use, so I will not give an example here ~~

The above is a detailed explanation of the Python JSON parsing content. For more information, see other related articles in the first PHP community!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.