[python] json.dumps() and json.loads() usage

1. Introduction to JSON

JSON stands for JavaScript Object Notation. It is a lightweight data interchange format for storing and exchanging data. It is a language-independent format that is very easy to understand because it is inherently self-describing. There is a built-in package in python that supports JSON data called json. Data in JSON is represented as quoted-strings, consisting of key-value maps between curly braces {}. Generally speaking, it is a data processing module that is easy to use in the interface, but json is not a data format.

2. Mapping of Python and Json data types

JSON Python
object dict
array list
string str
number int
true True
false False
null None

3. The difference between json.load(s) and json.dump(s)

json.load: means to read the file, return the python object
json.dump: means to write the file, the file is in json string format, no return
json.dumps : convert the dictionary type in python to string type, return the json string json.loads : convert the json string to a dictionary type, and return the python object load and dump are mainly processed by loads and dumps[dict→str]
[str→dict]
文件
字符串

insert image description here

json.load() reads data from a json file
json.loads() converts str type data to dict type
json.dumps() converts dict type data into str
json.dump() converts data to json data type into the file
insert image description here

4. Test

4.1 json.dumps()

import json

data = {
    
    
    'fruit':'apple',
    'vegetable':'cabbage'
}
print(data,type(data))

data = json.dumps(data)  # dict转json
print(data,type(data))

return:

{'fruit': 'apple', 'vegetable': 'cabbage'} <class 'dict'>
{"fruit": "apple", "vegetable": "cabbage"} <class 'str'>

4.2 json.loads()

data = """{
"fruit": "apple",
"vegetable": "cabbage"
}"""
# 一般此时data为request.text返回值
print(data, type(data))
data = json.loads(data)
print(data, type(data))

return:

{
"fruit": "apple",
"vegetable": "cabbage"
} <class 'str'>
{'fruit': 'apple', 'vegetable': 'cabbage'} <class 'dict'>

4.3 json.dump()

1. Write
in str a.py:

data = "wyt"
with open('b.json', 'w') as f:
    json.dump(data, f)

with open('b.json','r',encoding='utf-8') as f :
    f_str = json.load(f)
    print(f_str,type(f_str))

return:

wyt <class 'str'>

2. Write dict
a.py:

data = {
    
    
    'fruit':'apple',
    'vegetable':'cabbage'
}
with open('b.json', 'w') as f:
    json.dump(data, f)

with open('b.json','r',encoding='utf-8') as f :
    f_str = json.load(f)
    print(f_str,type(f_str))

return:

{'fruit': 'apple', 'vegetable': 'cabbage'} <class 'dict'>

4.4 json.load()

Exists in a.json:

{
    
    
"fruit": "apple",
"vegetable": "cabbage"
}

a.py:

with open('a.json','r',encoding='utf-8') as f :
    f_str = json.load(f)
    print(f_str,type(f_str))

return:

{'fruit': 'apple', 'vegetable': 'cabbage'} <class 'dict'>

Five, error analysis

5.1 Native code

data = '''{
'fruit':'apple',
'vegetable':'cabbage'
}'''
data = json.loads(data)

5.2 Error return

insert image description here

json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 2 column 1 (char 2)

5.3 Error analysis and solution

Double quotes are used inside json.

data = """{
"fruit": "apple",
"vegetable": "cabbage"
}"""
data = json.loads(data)
print(data, type(data))

return:

{'fruit': 'apple', 'vegetable': 'cabbage'} <class 'dict'>

Guess you like

Origin blog.csdn.net/qq_45859826/article/details/124158012