Python-json related operations

Serialization and deserialization

Before understanding json, you need to know serialization and deserialization

Serialization : The process of converting the state of an object into a form that can be stored or transmitted. To put it bluntly, it is to persist the object.

Deserialization : The opposite process of serialization, converting stored bytes into objects (in python for example: dictionaries, lists).

Both dump and dumps in json belong to serialization

dump: convert dict type to json string format, write to file  (easy to store)

import json
with open("test.json","w") as f:
    json.dump(dict,f)

dumps: convert dict type to string.

dict = {'marry':'hi','qiqi':'ok'}
str = json.dumps(dict)


Both load and loads in json belong to deserialization

load: Open the file and convert the string to a data type.

with open("test.json",'r') as fp:
     load_dict = json.load(fp)

loads: Convert str string type to dict dictionary type.

 dict = json.loads(str)

Guess you like

Origin blog.csdn.net/m0_54219225/article/details/121563341