Serialization and deserialization of Json--pickle

Source: Internet
Author: User

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:
    1. The son value supports a simple data type, and pickle supports all data types.
    2. Pickle can only support serialization and deserialization of Python itself and cannot be used as a data interaction with other languages, and JSON can.
    3. 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.
    4. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.