Introduction to serialization and deserialization of JSON
1, JSON can only serialize simple data types, such as lists, dictionaries, strings, etc. simple types, cannot serialize complex types.
2, JSON is supported in all languages, and we use JSON when we cross the language, (interacting with other languages)
JSON serialization
Serialization of JSON
Json.dumps ()
Import JSON
info = {
' Zhang ': ' 123 ',
' Qing ': ' 456 '
}
f = open (' Json_wenjian ', ' W ', encoding= ' utf-8 ')
F.write (Json.dumps (info))
Deserialization of JSON
Json.loads ()
Import jsonf = open (' Json_wenjian ', ' R ', encoding= ' utf-8 ') data = Json.loads (F.read ()) print (data)
JSON summary
Loads------dumps and load-------dump is in pairs.
The way is only a bit different.
The serialization of preface Pickle
Pickle can serialize all the data types in Python, including functions, classes, and so on, let's take a look at how the functions are serialized. What's more, pickle serializes bytes, and JSON serializes characters, and this is a bit of a note.
Serialization and deserialization of pickle
Serialization of
Import Pickledef Fun (): print (' hello,world ') info = { ' Zhang ': 123, ' Qing ': 456, ' Yao ': fun}with open (' Pickle_wenjian ', ' WB ') as f: data = pickle.dumps (info) f.write (data)
Deserialization
Import Pickledef Fun (): print (' Hello,world ') with open (' Pickle_wenjian ', ' RB ') as f: data = Pickle.loads (f.read ()) Print (data)
Summary:
- The son value supports a simple data type, and pickle supports all data types.
- Pickle can only support serialization and deserialization of Python itself and cannot be used as a data interaction with other languages, and JSON can.
- Pickle serializes the entire data object, so when deserializing the function, the logic in the function body changes, and is followed by the function body of the heart.
- Pickle and JSON in 3.0 can only dump once and load once, in 2.7 can dump several times, load several times, anyway, only remember, only need to dump once, load once.
Serialization and deserialization of Json--pickle