Home > Backend Development > Python Tutorial > Get to know python's json.dumps() and json.loads()

Get to know python's json.dumps() and json.loads()

coldplay.xixi
Release: 2020-12-17 17:35:56
forward
3501 people have browsed it

python video tutorialThe column introduces the two concepts of dumps() and loads()

Get to know python's json.dumps() and json.loads()

Related free learning recommendations: python video tutorial

1. Concept understanding

1. json.dumps() and json.loads() are json format processing functions (it can be understood that json is a string)
(1) The json.dumps() function is to process a Python data type list Encoding in json format (you can understand it this way, the json.dumps() function converts the dictionary into a string)
 (2) The json.loads() function converts the json format data into a dictionary (you can understand it this way, json. The loads() function converts a string into a dictionary)

2, json.dump() and json.load() are mainly used to read and write json file functions

二、Test

import json
# json.dumps()函数的使用,将字典转化为字符串
dict1 = {"age": "12"}
json_info = json.dumps(dict1)
print("dict1的类型:"+str(type(dict1)))
print("通过json.dumps()函数处理:")
print("json_info的类型:"+str(type(json_info)))
Copy after login
1 import json
2 
3 # json.loads函数的使用,将字符串转化为字典
4 json_info = '{"age": "12"}'
5 dict1 = json.loads(json_info)
6 print("json_info的类型:"+str(type(json_info)))
7 print("通过json.dumps()函数处理:")
8 print("dict1的类型:"+str(type(dict1)))
Copy after login
1 import json
2 
3 # json.dump()函数的使用,将json信息写进文件
4 json_info = "{'age': '12'}"
5 file = open('1.json','w',encoding='utf-8')
6 json.dump(json_info,file)
Copy after login
1 import json
2 
3 # json.load()函数的使用,将读取json信息
4 file = open('1.json','r',encoding='utf-8')
5 info = json.load(file)
6 print(info)
Copy after login

The above is the detailed content of Get to know python's json.dumps() and json.loads(). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template