Examples of dumps, loads, dump, and load functions in JSON

Source: Internet
Author: User

1.dumps ()

1. Json.dumps ()

To convert data from a dictionary (DIC) type to a string (str), an error occurs directly in writing the data of the Dict type to the JSON file, so the function needs to be used when writing the data.

ImportJSON name= {'AA':'1111','BB':'2222','cc':'3333','DC':'4444'} jsobj=json.dumps (name)Print(name)Print(jsobj)Print(Type (name))Print(Type (jsobj))

After the operation, the results are as follows;

{'AA':'1111','cc':'3333','BB':'2222','DD':'4444'}{"AA":"1111","cc":"3333","BB":"2222","DD":"4444"}<type'Dict'><type'Str'>

An error occurs when the JSON file is written to execute:

ImportJSON name= {'AA':'1111','BB':'2222','cc':'3333','DD':'4444'} emb_filename= ('/home/cqh/facedata/emb_json.json')    #jsobj = json.dumps (NAME_EMB)//When function conversion is not performedwith open (Emb_filename,"W") as F:f.write (name) f.close ()
When the function is not converted, the system error, and when the function is added, the system runs normally.

2.json.loads ()

Json.loads () is used to convert data of type STR to Dict.

ImportJSON Nameb= {'AA':'1111','BB':'2222','cc':'3333','D':'4444'} jsdumps=json.dumps (name) jsloads=json.loads (jsdumps)Print(name)Print(jsdumps)Print(jsloads)Print(Type (name))Print(Type (jsdumps))Print(Type (jsloads))

Operation Result:

{'AA':'1111','cc':'3333','BB':'2222','DD':'4444'}{"AA":"1111","cc":"3333","BB":"2222","DD":"4444"}{u'AA': U'1111', u'cc': U'3333', u'BB': U'2222', u'DD': U'4444'}<type'Dict'><type'Str'><type'Dict'>

3, Json.dump ()

Json.dump () is used to convert data of type dict to STR and write to a JSON file. There are two ways to write data to a JSON file

ImportJSON Name_emb= {'a':'1111','b':'2222','C':'3333','D':'4444'} emb_filename= ('/home/cqh/facedata/emb_json.json')   #Solution 1Jsobj =Json.dumps (NAME_EMB) with open (Emb_filename,"W") as F:f.write (jsobj) f.close ()#Solution 2Json.dump (NAME_EMB, open (Emb_filename,"W"))

4, Json.load ()

Json.load () is used to read data from a JSON file.

1 ImportJSON2  3Emb_filename = ('/home/cqh/facedata/emb_json.json')  4  5Jsobj =json.load (Open (emb_filename))6  7 Print(jsobj)8 Print(Type (jsobj))9  Ten  forKeyinchJsobj.keys (): One     Print('Key:%s Value:%s'% (Key,jsobj.get (key)))

The results of the operation are as follows:

{u'a': U'1111', u'C': U'3333', u'b': U'2222', u'D': U'4444'}<type'Dict'>key:a Value:1111key:c Value:3333key:b Value:2222key:d Value:4444

Examples of dumps, loads, dump, and load functions in JSON

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.