Python json module and pythonjson Module

Source: Internet
Author: User

Python json module and pythonjson Module

  

Import json

Data = {
"No": 1,
"Name": "Runoob ",
"Url": "http://www.runoob.com"
}

Json_str = json. dumps (data) # The json. dumps () function encodes the data and converts it into json-formatted data.
Print ("python raw data:", repr (data ))
Print ("python json data:", json_str)
Python_data = json. loads (json_str) # The json. loads () function is used to decode data in json format and convert it into data in the corresponding python format.
Print (python_data)


Output the corresponding result:

Python raw data: {'no': 1, 'name': 'runoob', 'url': 'http: // www.runoob.com '}


Python json data: {"no": 1, "name": "Runoob", "url": "http://www.runoob.com "}


{'No': 1, 'name': 'runoob', 'url': 'http: // www.runoob.com '}

 

 

 

# This is about writing formatted data to a file and reading data from the file.
F = open ("data.txt", "w", encoding = "UTF-8 ")
F. write (json. dumps (data ))
F. close ()
F1 = open ("data.txt", "r", encoding = "UTF-8 ")
F1 = f1.read ()
Json_str1 = json. loads (f1)
Print ("-----")
Print (json_str1)

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.