Python objects and JSON convert to each other __JS

Source: Internet
Author: User
Tags true true
One, Python support for JSON

Starting with python2.6, the Python standard library has added support for JSON, which requires only import JSON when manipulating JSON. two, Python objects converted to JSON strings

When converting a Python object to a JSON string, you only need the following knowledge:

Conversion rules for 1.python objects to JSON strings:

Python JSON
Dict Object
List, tuple Array
STR, Unicode String
int, long, float Number
True True
False False
None Null

2. The main use of the following functions:

Json.dumps ()

The specific parameters of the function (see the link in the appendix):
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

Test code:

#构造字典
Python2json = {}
#构造list
listdata = [1,2,3]
python2json["listdata"] = Listdata
Python2json ["strdata"] = "Test python obj 2 json"

#转换成json字符串
json_str = Json.dumps (Python2json)
print Json_str

Conversion results:

{
    "listdata": [
        1,
        2,
        3
    ],
    "strdata": "Test Python obj 2 json"
}
Third, JSON string converted to Python object

In the same vein, converting a JSON string into a Python object requires only the following knowledge:

Conversion rules for 1.json strings to Python objects:

JSON Python
Object Dict
Array List
String Unicode
Number (int) int, long
Number (real) Float
True True
False False
Null None

2. The main use of the following functions:

Json.loads ()

The specific parameters of the function (see the link in the appendix):
Json.loads (s[, encoding[, cls[, object_hook[, parse_float[, parse_int[, parse_constant[, object_pairs_hook[, **kw]]]] ]]) example

Test code:

str = ' {listdata ': [1, 2, 3], "strdata": "Test Python obj 2 json"} '
Json2python = json.loads (str)
print type (JSON 2python)

Conversion results:

<type ' Dict ' >
Iv. Appendix

Https://docs.python.org/2/library/json.html
Http://www.cnblogs.com/loleina/p/5623968.html

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.