Home > Article > Backend Development > Python summary of json standard library
This article brings you relevant knowledge about python, which mainly introduces related issues about the json standard library, including a basic overview of JSON, json modules, XML files and JSON file interaction Let’s take a look at the transferred content below. I hope it will be helpful to everyone.
Recommended learning: python video tutorial
JSON (Full name: JavaScript Object N otation object representation) is a lightweighttextdataexchange format. The data format of JSON is actually the dictionary format in Python, which can contain An array enclosed in square brackets is a list in Python.
Grammar format : {key1:value1, key2:value2,} Key-value pair format (separated by colon), use commas to connect the pairs
Simple case: JSON object
{ "name": "小明", "age": 18}
Complex case: JSON array
{ "student": [ {"name": "小明", "age": 11}, {"name": "小红","age": 10} ], "classroom": {"class1": "room1", "class2": "room2"}}3. Notes1. The
key part of the json key-value pair must be wrapped in double quotes ". Single quotes will not work (so if keywords appear in the key, they will also be characterized). There are no mandatory requirements for objects in js (so keywords are not allowed in keys).
value part of the json key-value pair does not allow functions, undefined, NaN, but can have null, which can appear in the value of the object in js.
3. After the json data ends,meaningless commas are not allowed to appear, such as: {" name":"admin","age":18,}, pay attention to the comma after 18 at the end of the data, it is not allowed.
The correct json format is as follows:
# 格式1:JSON 对象{"name": "admin", "age": 18}# 格式2:JSON 数组{ "student": [ {"name": "小明", "age": 18}, {"name": "小红", "age": 16}, {"name": "小黑", "age": 20} ]}
The incorrect json format is as follows:
Python to json format. There will be changes in the data type, as follows The table shows:
JSON | |
---|---|
object | |
array | ##str |
int, float, int- & float-derived Enums | |
##True | |
False | |
None | |
In turn, convert the json format into python built-in types, as shown in the following table: |
array | |
string | |
number(int) | |
number(real) | |
##true | True |
false | False |
null | None |
3. Usage method |
Method
|
json.dump(obj, fp) |
|
json.loads(s) |
|
json.load(fp) |
|
The above is the detailed content of Python summary of json standard library. For more information, please follow other related articles on the PHP Chinese website!