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数据类型的映射

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

复制代码

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐