The difference between the four functions Json.dumps () and Json.loads (), Json.dump (), and Json.load () involved in JSON file processing in Python

Source: Internet
Author: User

I. Conceptual understanding

1, Json.dumps () and json.loads () are JSON format processing functions (so to understand, JSON is a string)
(1) the Json.dumps () function is an encoding of a Python data type list in JSON format (so to understand that the json.dumps () function converts a dictionary to a string)
(2) the Json.loads () function converts the JSON format data to a dictionary (as you can understand, the json.loads () function converts a string into a dictionary)

2, Json.dump () and Json.load () are mainly used to read and write JSON file functions

Second, the Code test

1.py

1 ImportJSON2 3 #use of the Json.dumps () function to convert the dictionary to a string4Dict1 = {" Age":" A"}5Json_info =json.dumps (Dict1)6 Print("type of Dict1:"+str (type (DICT1)))7 Print("handled by the Json.dumps () function:")8 Print("type of Json_info:"+str (Type (json_info)))

Run:

2.py

1 ImportJSON2 3 #use of the Json.loads function to convert a string into a dictionary4Json_info ='{"Age": " " "}'5Dict1 =json.loads (json_info)6 Print("type of Json_info:"+str (type (json_info)))7 Print("handled by the Json.dumps () function:")8 Print("type of Dict1:"+str (Type (DICT1)))

Run:

3.py

 1  import   JSON  2  3  #   Json.dump () function, write JSON information into file  4  Json_info =  " {' age ': ' '"}  "  5  file = open (" Span style= "COLOR: #800000" >1.json   ",   w   ", encoding=  " utf-8   " )  6  json.dump (json_info,file) 

Run (1.json file):

4.py

1 Import JSON 2 3 # The use of the Json.load () function will read the JSON information 4 File = open ('1.json','r', encoding='  utf-8')5 info = json.load (file)6  Print(info)

Run:

The difference between the four functions Json.dumps () and Json.loads (), Json.dump (), and Json.load () involved in JSON file processing in Python

Related Article

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.