Python中json.dump()和json.dumps()的区别

一、图解

json.dumps(dict, indent):将Python对象转换成json字符串 
json.dump(dict, file_pointer):将Python对象写入json文件

 

 

二、json.dumps()用法

1、用法

json.dumps(dict, indent):将Python对象转换成json字符串

2、参数
dict:被转换的名称 
indent:打印格式的参数

3、例子

复制代码
import json

dictionary ={
"id": "04",
"name": "sunil",
"depatment": "HR"
}

json_object = json.dumps(dictionary, indent=4)
print(json_object)
复制代码


2、Python和Json数据类型的映射

PythonJSON Equivalent
dict object
list, tuple array
str string
int, float number
True true
False false
None null

 

三、json.dumps()用法

1、用法

json.dump(dict, file_pointer):将Python对象写入json文件

2、参数
dict:被转换的名称 
file_pointer:打开文件的指针

3、例子

复制代码
import json

dictionary = {
"id": "04",
"name": "sunil",
"depatment": "HR"
}

with open("jsons_txt", "w") as filedata:
json.dump(dictionary, filedata)
复制代码
posted @ 2022-12-14 09:01  侬侬发  阅读(295)  评论(0编辑  收藏  举报