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 |
|
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 |
|
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