Python STL JSON

Source: Internet
Author: User
Tags comparison table

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, cls=None, indent=None, separators=None, encoding="utf-8", default=None, sort_keys=False**kw)
Instance

The following example encodes an array into JSON-formatted data:

#!/usr/bin/pythonimport='a'1'b'2'c'3'd'4'e'5= json.dumps(data)print 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:

>>>import json>>>print json.dumps({'a''Runoob''b'7}, sort_keys=True, indent=4, separators=(','': ')){    "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
**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'1u'c'3u'b'2u'e'5u'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

For more information: https://docs.python.org/2/library/json.html.

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:

pip install demjson

More installation Information view: Http://deron.meranda.us/python/demjson/install

Demjson function
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/pythonimport='a'1'b'2'c'3'd'4'e'5= demjson.encode(data)print 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'1u'c'3u'b'2u'e'5u'd'4}

Python STL JSON

Related Article

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.