API
API |
Annotation |
Json.dumps () |
Converts a dictionary in Python to a string |
Json.loads () |
Convert a string to a dictionary |
Json.dump (,) |
Write data to the JSON file |
Json.load () |
To open the file and transform the string into a data type |
Test
Import JSON
dict_1 = {' City ': ' Nanjing ', ' Year ': 2018, ' Province ': ' Jiangshu '}
# dumps: Convert a dictionary in Python to a string
str_1 = Json.dumps (dict_1)
assert isinstance (str_1, str)
# Loads: Convert a string to a dictionary
dict_2 = json.loads (str_1)
assert isinstance (dict_2, dict)
# Dump: Writes data to the JSON file
with open ("./test.json", "W") as Dump_f:
json.dump ( Dict_1, Dump_f)
# Load: Opens the file and transforms the string to the data type with
open ("./test.json", "R") as Load_f:
dict_3 = Json.load ( Load_f)
assert Isinstance (Dict_3, Dict)
[1] Python reads and writes JSON files