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)


浙公网安备 33010602011771号