Preface
json is a lightweight data exchange format created by JavaScript language. It is widely used in web page data interaction and is commonly used in the fields of crawlers and data analysis.
The json format is concise and has a clear structure. The storage format is: key-value pair (key:value).
In Python, the dictionary (dict) is very similar to json, both in the form of key-value pair (key:value).
1. The difference between json and dictionary
The difference between json and dictionary is as follows:
json format | dict (dict, python) |
---|---|
A data format, pure string. Can be parsed into python dictionary (dict) form or other forms. | a data structure |
key can only be a string | The key can be any immutable data in python: string, value, tuple, etc. |
Keys can be ordered and repeated | key cannot be repeated |
Strings can only use double quotes | Strings can use single quotes, double quotes, or triple quotes. |
Any key has a default value of undefined | Any key has no default value |
json:true、false、null、string | dict:Ture、False、None、str |
json Chinese is unicode encoding, such as "\u6211" |
2. Conversion between json and dictionary
First, create a dictionary (a_dict), as an example.
a_dict = {
'a': 1, "b": 'qw', '''c''': ['q', 'w'], 'd': '您好'}
print(type(a_dict))
print(a_dict)
(1) Convert dictionary into json format data
json.dumps(): Convert python data type into json string, dict→json
syntax:
json.dumps(obj, ensure_ascii=True,indent=None,encoding=“utf8”,sort_keys=False,separators=None…)
Common parameters | Definition |
---|---|
ensure_ascii | The default is True, and Chinese encoding is unicode; if it is changed to False, Chinese characters can be output. |
indent | The default is None, and the parameters are displayed indented according to the format, indicating how many spaces are indented, and the int type value is received. |
sort_keys | The default is False, not sorting by dictionary key; change to True, sorting by dictionary key |
separators | Specify the separator, the default is None; includes the separator between each element of dict (comma ","), the separator between key and value (colon ":"), after specifying the separator, the original separator will be removed directly |
encoding | Encoding, default utf-8 |
Convert the dictionary a_dict into json format data, the code is as follows:
# 将字典a_dict转换为json
import json
a_dict = {
'a': 1, "b": 'qw', '''c''': ['q', 'w'], 'd': '您好'}
a_json = json.dumps(a_dict)
print(type(a_json))
print(a_json)
Note: After the dictionary a_dict is converted into json data a_json, there are the following changes:
(1) The data type changes from dictionary dict to str
(2) Single quotes and triple single quotes in a_dict are all unified into double quotes
(3) Chinese "Hello" is converted to unicode encoding "\u60a8\u597d".
If you want to display Chinese normally, you can add the parameter: ensure_ascii=False, and the code becomes:
# 将字典a_dict转换为json
import json
a_dict = {
'a': 1, "b": 'qw', '''c''': ['q', 'w'], 'd': '您好'}
a_json = json.dumps(a_dict, ensure_ascii=False)
print(type(a_json))
print(a_json)
(4) If there is a lot of data, it is difficult to see the structure clearly if it is displayed row by row. Add parameters: indent=number of indent spaces, which can produce beautiful and clear output. The code is as follows:
import json
a_dict = {
'a': 1, "b": 'qw', '''c''': ['q', 'w'], 'd': '您好'}
a_json = json.dumps(a_dict, ensure_ascii=False, indent=2) # 缩进2个空格
print(type(a_json))
print(a_json)
(2) Convert json format data into dictionary
json.loads(): Parses the json string into python data type, json→dict
converts a_json into dictionary format data, as follows:
b_dict = json.loads(a_json)
print(type(b_dict))
print(b_dict)
The above is the difference between json and dictionary data formats in Python, as well as mutual conversion methods, for reference.
-end-