Home  >  Article  >  Backend Development  >  Get to know python’s json.dumps() and json.loads()

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

coldplay.xixi
coldplay.xixiforward
2020-12-17 17:35:563296browse

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)))
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)))
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)
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)

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!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete