What is the difference between json.dumps and json.load?
source link: https://www.codesd.com/item/what-is-the-difference-between-json-dumps-and-json-load.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
What is the difference between json.dumps and json.load?
What is the difference between json.dumps
and json.load
?
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
would take a file-like object, read the data from that object, and use that string to create an object:
with open('file.son') as fh:
a = json.load(fh)
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
functions as wrappers around the s
-less functions:
def dump(obj, fh):
fh.write(dumps(obj))
def load(fh):
return loads(fh.read())
Related Articles
JQuery - What are the differences between $ (document) .ready and $ (window) .load?
What is the difference between $ _SERVER ['PATH_INFO'] and $ _SERVER ['ORIG_PATH_INFO']?
What is the difference between long long and long
What is the difference between robot-android and robot-indifferent?
What is the difference between char * val and char *** val?
What is the difference between window.jQuery and if (type of jQuery == & ldquo; undefined & rdquo;) when resuming?
What is the difference between low equity and strong equity?
What is the difference between String.Empty and & ldquo; & Rdquo; (Empty string)?
What is the difference between smoke tests and mental health tests?
What are the differences between Vanilla Perl and Strawberry Perl?
Swift: What is the difference between metatype .Type and .self?
What is the difference between a packet and a socket?
What are the differences between a UIView and a CALayer?
What is the difference between AWS Marketplace and sudo apt-get install?
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK