3

What is the difference between json.dumps and json.load?

 3 years ago
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.
neoserver,ios ssh client

What is the difference between json.dumps and json.load?

advertisements

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 are differences between $(document).ready(function(){ //my code here }); and $(window).load(function(){ //my code here }); And I want to make sure that: $(document).ready(function(){ }) and $(function(){ }); and jQuery(document).ready(function()

What is the difference between $ _SERVER ['PATH_INFO'] and $ _SERVER ['ORIG_PATH_INFO']?

What's the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? How do I use them? When I run print_r($_SERVER), PATH_INFO and ORIG_PATH_INFO are not present in the array. Why not? How can I enable them? I have read the PHP manual

What is the difference between long long and long

What's the difference between long long and long? And they both don't work with 12 digit numbers (600851475143), am I forgetting something? #include <iostream> using namespace std; int main(){ long long a = 600851475143; } On major 32-bit platforms:

What is the difference between robot-android and robot-indifferent?

I'm developing an Android app that requires to use Roboto font to render the UI. According to https://github.com/google/roboto/releases, there are three types of Roboto font: Google says: Hinted fonts Hints are the instructions embedded in a font on

What is the difference between char * val and char *** val?

This question already has an answer here: Uses for multiple levels of pointer dereferences? 17 answers What is the difference between char *val and char ***val. I know what pointers are but can not find anywhere what this triple star notation means.E

What is the difference between window.jQuery and if (type of jQuery == & ldquo; undefined & rdquo;) when resuming?

I want to load jQuery with a fallback solution and have come across two different solutions: 1: <script src="//ajax.googleapis.com/ [...] jquery.js"></script> <script> window.jQuery || document.write('<script src="js/jq

What is the difference between low equity and strong equity?

What is the difference between weak fairness and strong fairness? What would be an example, which contains set of variables and set of actions?Given a transition graph, weak fairness ensures that the execution cannot remain forever in a state where t

What is the difference between String.Empty and & ldquo; & Rdquo; (Empty string)?

In .NET, what is the difference between String.Empty and "", and are they interchangable, or is there some underlying reference or Localization issues around equality that String.Empty will ensure are not a problem?In .NET prior to version 2.0,

What is the difference between smoke tests and mental health tests?

What is the difference between smoke testing and sanity testing? When do will perform smoke testing and when do will perform sanity testing?Sanity testing Sanity testing is the subset of regression testing and it is performed when we do not have enou

What are the differences between Vanilla Perl and Strawberry Perl?

Strawberry Perl is "Open Source Perl for Windows that is exactly the same as Perl everywhere else". Vanilla Perl "provides a Perl distribution that is as close to the Perl core as possible." Strawberry Perl is built on Vanilla Perl. Bo

Swift: What is the difference between metatype .Type and .self?

What's the difference between metatype .Type and .self in Swift? Do .self and .Type return a struct? I understand that .self can be used to check with dynamicType. How do you use .Type?Here is a quick example: func foo<T>(type: T.Type) { print("

What is the difference between a packet and a socket?

I just read "What is the difference between a port and a socket?" and seems socket is something to create connections. And then how about a packet? Is something sending between the connection? So the progress is "ip -> port -> socket

What are the differences between a UIView and a CALayer?

Both have most of the same attributes, both support different kind of animations, both represent different data. What are the differences between a UIView and a CALayer?On iOS, every UIView is backed by a Core Animation CALayer, so you are dealing wi

What is the difference between AWS Marketplace and sudo apt-get install?

I'm seeking objective answers, so that this is not closed as subjective. Question may be moved to a different site if required. What is the difference between AWS Marketplace and sudo apt-get install, and how do I decide to choose one over the other?

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK