We are doing work often use to json modules, today a brief introduction json module
What is json
The JSON, called the JavaScript Object Notation, JavaScript Object Notation i.e., a combination of which is represented by m array of objects and data, but the structure is simple configuration a very high degree, is a lightweight data-interchange format. json module in python as data analysis json
Objects and arrays
In the JavaScript language, everything is an object. Any type of support can be represented by the JSON, such as strings, numbers, objects, and other arrays, but the special objects and arrays are two types of common and
Object : It is used in JavaScript braces {} wrapped content, data structure {keyl: valuel, key2: value2 } The key structure. In object-oriented languages, key property of the object, value for the corresponding value. Keys can be used to represent integers and strings. Type values may be of any type.
Array : An array is square brackets [] content wrapped in JavaScript, the data structure [ "java", "javascript" , "vb"] index structure. In JavaScript, the array is a special type of data, it can also be use as key-value pairs like objects, but still much more useful index. Similarly, the type can be any type of value.
JSON object format
And we will find the dictionary format is not the same as special
# 格式为{"name":"value"}
[{"name":"AnJing","age":"26" }]
JSON is simple to use
loads () method
Use loads () letters into the json json objects
# Coding: UTF. 8- Import JSON STR = '' ' [{ "name": "AnJing", "Age": "26 is" }] ' '' Data = json.loads (STR) Print (Data) Print (type (Data)) Print (Data [0] [ ' name ' ]) # selected by the subscript Print (Data [0] .get ( ' name ' )) Code results: [{ ' name ' : ' AnJing ' , 'age': '26'}] <class 'list'> AnJing AnJing
Note: When using loads of json string must use double quotation marks, otherwise it will error
(json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 3 column 5 (char 12))
dumps () method
Use dumps () method is converted into a string json
# coding:utf-8 import json str = [{ "name":"AnJing", "age":"26" }] print(type(str)) data = json.dumps(str) print(type(data)) print(data) 代码结果: <class 'list'> <class 'str'> [{"name": "AnJing", "age": "26"}]
Also if you want to save the text look good points, we can add a parameter indent, on behalf of the indentation number of characters, when already written Chinese is not garbled, adding ensure_ascii = False
# Coding: UTF. 8- Import JSON STR = [{ " name " : " Quiet " , " Age " : " 26 is " , " Gender " : " M " }] # indent for indentation # ensure_ascii = False for write Chinese Open with ( ' 123.json ' , ' W ' , encoding = ' UTF-. 8 ' ) AS F: f.write (json.dumps (STR,indent2 =, ensure_ascii = False)) Code Results: [ { " name " : " Quiet " , " Age " : " 26 is " , " Gender " : " M " } ]
load () method
Convert the file contents into json data
# Import module json Import json STR = [{ " name " : " AnJing " , " Age " : " 26 is " }] # by writing a file dumps with Open ( ' 123.json ' , ' W ' ) AS F: f.write (json.dumps (STR)) # by converting the data into load JSON data = the json.load (Open ( ' 123.json ' , ' R & lt ')) print(data) Code Results: [{'name': 'AnJing', 'age': '26'}]
dump () method
Json data is written to a file
# Coding: UTF. 8- Import json STR = [{ " name " : " AnJing " , " Age " : " 26 is " }] # written by the dump into json The json.dump (STR, Open ( ' 123.json ' , ' W ' )) # by writing to see if the load Data = the json.load (Open ( ' 123.json ' , ' R & lt ')) Print (Data) Code Results: [{ 'name': 'AnJing', 'age': '26'}]
Simply write write json module to use, as well as a brief introduction json, please continue to add knowledge not covered.
Write your sense of help, a point not lost focus on ~ ~ ~ ~