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)