json.dumps和json.load有什么区别? [英] What is the difference between json.dumps and json.load?
本文介绍了json.dumps和json.load有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
json.dumps
和json.load
有什么区别?
根据我的理解,一个将JSON加载到字典中,另一个则将其加载到对象中.
From my understanding, one loads JSON into a dictionary and another loads into objects.
推荐答案
dumps
takes an object and produces a string:
>>> a = {'foo': 3}
>>> json.dumps(a)
'{"foo": 3}'
load
将采用类似文件的对象,请阅读来自该对象的数据,并使用该字符串创建一个对象:
load
would take a file-like object, read the data from that object, and use that string to create an object:
with open('file.json') as fh:
a = json.load(fh)
请注意, dump
和load
在文件之间转换和对象,而dumps
和 loads
在字符串和对象.您可以将s
-less函数视为s
函数的包装器:
Note that dump
and load
convert between files and objects, while dumps
and loads
convert between strings and objects. You can think of the s
-less functions as wrappers around the s
functions:
def dump(obj, fh):
fh.write(dumps(obj))
def load(fh):
return loads(fh.read())
这篇关于json.dumps和json.load有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文