JSON of data conversion

Source: Internet
Author: User

1. Definition: Json (JavaScript object Notation) is a lightweight data teach-in mode, in short, the objects and arrays in JavaScript, so these two structures are objects and arrays of two structures, through the two structures can represent a variety of complex structures.

    • Object: object in JS is represented as {} content , data structure is {key:value, Key:value,.........} Key-value pair, in object-oriented language, key is the property of the object, value is the corresponding property value, value method: Object. Key Gets the property value, which can be a number, a string, an array, an object.
    • Array: The array in JS is [] in parentheses, the data structure is ["Python", "JavaScript", "C + +",......] in the same way as in all languages, using the index, the type of the field can be numbers, strings, arrays, objects, and so on.

2.import JSON #调用json模块

The JSON module provides four functions: dumps, dump, loads, load, for string and Python data conversions

L json.loads (): Used to translate JSON format strings into Python objects, from JSON to Python type conversion references are as follows:

Json

Pyhon

  • Object

Dict

Array

List

String

Unicode

Number (int)

Int,long

Number (real)

Float

True

False

Null

None

Use effect:

Import JSON

Strlist = ' [1, 2, 3] '

Strdict = ' {' City ': ' Beijing ', ' name ': ' Big Cat '}

Json.loads (strlist) = = "changed to [1, 2, 3]

Json.loads (strdict) = = "Into Unicode format, {ucity ': ' \u531\y4eac ', ' name ': ' \u5927\u732b '}

L Json.dumps (): Implements the Python type into a JSON string, returns a stone man object, converts the Python object encoding into a JSON string. The data conversion comparison is as follows:

Pyhon

Json

Dict

  • Object

List,tuple

Array

Unicode,str

String

Int, long, float

Number

True

True

False

Flase

None

Null

Note: json.dumps () using ASCII encoding by default requires us to convert to UTF-8 encoding,

This requires us to add a parameter: ensure_ascii==flase

JSON of data conversion

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.