API
API |
Annotation |
json.dumps( ) |
Convert dictionary in python to string |
json.loads( ) |
convert string to dictionary |
json.dump( , ) |
write data to json file |
json.load( ) |
Open the file and convert the string to the data type |
Test
import json
dict_1 = {"City":"Nanjing", "Year":2018, "Province":"Jiangshu"}
str_1 = json.dumps(dict_1)
assert isinstance(str_1, str)
dict_2 = json.loads(str_1)
assert isinstance(dict_2, dict)
with open("./test.json", "w") as dump_f:
json.dump(dict_1, dump_f)
with open("./test.json", "r") as load_f:
dict_3 = json.load(load_f)
assert isinstance(dict_3, dict)
[1] python read and write json files