Python standard library json library

Table of contents

1. Introduction

2. Method

3. Data format


1. Introduction

Json, the full name of JavaScript Object Notation, is JavaScript object markup. It represents data through a combination of objects and arrays. Although the structure is simple, it is highly structured. It is a lightweight data exchange format. The most widely used application of Json is as a data format for communication between web servers and clients in AJAX. Nowadays, it is also commonly used in http requests, so it is natural to learn all kinds of json.

2. Method

It is mainly used to encode python objects into json format for output or storage, and to decode json format objects into python objects.

method describe
json.dump() Pass in a python object, encode it into json format and store it in the IO stream
json.dumps() Pass in a python object, encode it into json format and store it in str
json.load() Pass in a json format file stream and decode it into a python object
json.loads() Pass in a str in json format and decode it into a python object

1.json.loads()

Usage: Pass in a str in json format and encode it into a python object

eg:

A string in the form of JSON, which is of str type. We use json.loads to convert it into a python data structure and turn it into a list or dictionary, so that we can operate it.

import json

data = '''
[{
    "name": "小明",
    "height": "170",
    "age": "18"
}, {
     "name": "小红",
    "height": "165",
    "age": "20"
}]
'''

# 打印data类型
print(type(data))
# json类型的数据转化为python类型的数据
new_data = json.loads(data)
# 打印data类型
print(type(new_data))

operation result:

 In this way, we can use the index to obtain the corresponding content. For example, if we want to obtain the name attribute in the first element, we can use the following method:

import json

data = '''
[{
    "name": "小明",
    "height": "170",
    "age": "18"
}, {
     "name": "小红",
    "height": "165",
    "age": "20"
}]
'''

# json类型的数据转化为python类型的数据
new_data = json.loads(data)
# 获取内容
name = new_data[0]['name']
new_name = new_data[0].get('name')
# 打印转换后data类型

print(name)
print(new_name)

operation result:

2.json.load()

Usage: Pass in a json format file stream and encode it into a python object

eg:

The load method operates on the entire file object. Here, the contents of the entire file object are converted into json objects. (The picture below is the file operation object)

import json

# 构建该文件的文件对象
with open('test1.json',encoding='utf-8')as fp:
    # 加载垓文件对象,转换为python类型的数据
    pyth_list = json.load(fp)
    print(pyth_list)
    print(type(pyth_list))
    print(type(pyth_list[0]))


operation result:

2.json.dumps()

Convert python type data into json string

Insert image description here

 dumps function

import json

data = '''
[{
    "name": "小明",
    "height": "170",
    "age": "18"
}, {
     "name": "小红",
    "height": "165",
    "age": "20"
}]
'''

# 打印要来data类型
print(type(data))
# json类型的数据转化为python类型的数据
new_data = json.loads(data)

# 把python类型的数据转换成json字符串
lit = json.dumps(new_data)

# 打印转换后data类型
print(type(new_data))

print(type(lit))

operation result:

In fact, the usage of loads and dumps are the same. Loads converts json type data into python type data, while dumps converts json type data into python type data. It is equivalent to one encoding and one decoding.

2.json.dump()
stores python type data into a file in json format

Insert image description here

eg:

In order to output Chinese, you also need to specify the parameter ensure_ascii as False.

We found a problem, that is, after converting it into a json string, the Chinese display was garbled.
This is because when json is serialized, the default encoding is ASCII, and Chinese is Unicode encoding. ASCII does not contain Chinese, so garbled characters appear.
If you want json.dumps() to display Chinese normally, just add the parameter ensure_ascii=False , so that the default ASCII encoding will not be used when json is serialized.

3. Data format

A JSON object can be written as follows:

[{
    "name": "小明",
    "height": "170",
    "age": "18"
}, {
     "name": "小红",
    "height": "165",
    "age": "20"
}]

What is surrounded by square brackets is equivalent to a list type. Each element in the list can be of any type. In this example, it is a dictionary type, surrounded by curly brackets.

JSON can be freely combined from the above two forms, can be nested infinitely, has a clear structure, and is an excellent way to exchange data.

Guess you like

Origin blog.csdn.net/m0_63636799/article/details/130166379