什么是 JSON 中的反序列化和序列化?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3316762/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 17:33:03  来源:igfitidea点击:

What is deserialize and serialize in JSON?

jsonserializationdeserializationjson-deserializationjson-serialization

提问by coderex

I have seen the terms "deserialize" and "serialize" with JSON. What do they mean?

我在 JSON 中看到了术语“反序列化”和“序列化”。他们的意思是什么?

回答by kennytm

JSON is a format that encodes objects in a string. Serializationmeans to convert an object into that string, and deserialization is its inverse operation (convert string -> object).

JSON 是一种将对象编码为字符串的格式。序列化意味着将一个对象转换成那个字符串反序列化是它的逆操作(转换字符串 -> 对象)

When transmitting data or storing them in a file, the data are required to be byte strings, but complex objects are seldom in this format. Serialization can convert these complex objects into byte strings for such use. After the byte strings are transmitted, the receiver will have to recover the original object from the byte string. This is known as deserialization.

传输数据或存入文件时,要求数据为字节串,但复杂对象很少采用这种格式。序列化可以将这些复杂的对象转换为字节字符串以供此类使用。字节串传输后,接收方必须从字节串中恢复原始对象。这称为反序列化。

Say, you have an object:

说,你有一个对象:

{foo: [1, 4, 7, 10], bar: "baz"}

serializing into JSON will convert it into a string:

序列化为 JSON 会将其转换为字符串:

'{"foo":[1,4,7,10],"bar":"baz"}'

which can be stored or sent through wire to anywhere. The receiver can then deserialize this string to get back the original object. {foo: [1, 4, 7, 10], bar: "baz"}.

可以存储或通过电线发送到任何地方。然后接收者可以反序列化这个字符串以取回原始对象。{foo: [1, 4, 7, 10], bar: "baz"}.

回答by winklerrr

In the context of data storage, serialization(or serialisation) is the process of translating data structures or object state into a format that can be stored (for example, in a file or memory buffer) or transmitted (for example, across a network connection link) and reconstructed later. [...]
The opposite operation, extracting a data structure from a series of bytes, is deserialization. From Wikipedia

在数据存储的上下文中,序列化(或序列化)是将数据结构或对象状态转换为可以存储(例如,在文件或内存缓冲区中)或传输(例如,通过网络连接)的格式的过程链接)并稍后重建。[...]
从一系列字节中提取数据结构的相反操作是反序列化来自维基百科

In Python "serialization" does nothing else than just converting the given data structure (e.g. a dict) into its valid JSON pendant (object).

在 Python 中,“序列化”只是将给定的数据结构(例如 a dict)转换为其有效的 JSON 挂件(对象)。

  • Python's Truewill be converted to JSONs trueand the dictionary itself will then be encapsulated in quotes.
  • You can easily spot the difference between a Python dictionary and JSON by their Boolean values:
    • Python: True/ False,
    • JSON: true/ false
  • Python builtin module jsonis the standard way to do serialization:
  • PythonTrue将被转换为 JSON true,然后字典本身将被封装在引号中。
  • 您可以通过布尔值轻松发现 Python 字典和 JSON 之间的区别:
    • True/ False
    • JSONtrue/false
  • Python 内置模块json是进行序列化的标准方法:

Code example:

代码示例:

data = {
    "president": {
        "name": "Zaphod Beeblebrox",
        "species": "Betelgeusian",
        "male": True,
    }
}

import json
json_data = json.dumps(data, indent=2) # serialize
restored_data = json.loads(json_data) # deserialize

# serialized json_data now looks like:
# {
#   "president": {
#     "name": "Zaphod Beeblebrox",
#     "species": "Betelgeusian",
#     "male": true
#   }
# }

Source: realpython.com

来源:realpython.com

回答by Asif

Explanation of Serialize and Deserialize using Python

Python Serialize 和 Deserialize 的讲解

In python, pickle moduleis used for serialization. So, the serialization process is called picklingin Python. This module is available in Python standard library.

在python中,pickle模块用于序列化。因此,序列化过程在 Python 中称为酸洗。这个模块在 Python 标准库中可用

Serialization using pickle

使用 pickle 进行序列化

import pickle

#the object to serialize
example_dic={1:"6",2:"2",3:"f"}

#where the bytes after serializing end up at, wb stands for write byte
pickle_out=open("dict.pickle","wb")
#Time to dump
pickle.dump(example_dic,pickle_out)
#whatever you open, you must close
pickle_out.close()

The PICKLE file (can be opened by a text editor like notepad) contains this (serialized data):

PICKLE 文件(可以通过记事本等文本编辑器打开)包含以下内容(序列化数据):

}q (KX 6qKX 2qKX fqu.

}q (KX 6qKX 2qKX fqu.

Deserialization using pickle

使用pickle反序列化

import pickle

pickle_in=open("dict.pickle","rb")
get_deserialized_data_back=pickle.load(pickle_in)

print(get_deserialized_data_back)

Output:

输出:

{1: '6', 2: '2', 3: 'f'}

{1:'6',2:'2',3:'f'}