简体   繁体   English

JSON与序列化有何关系?

[英]How is JSON related to serialization?

What is serialization? 什么是序列化?

Serialization is the process of converting the object into stream of bytes to send it over a network or store it in a file in such a way that the object can be reconstructed again. 序列化是将对象转换为字节流以通过网络发送它或将其存储在文件中的过程,以便可以再次重建该对象。

What is JSON ? 什么是JSON

JSON is the data interchange format, having a fixed structure. JSON是数据交换格式,具有固定的结构。 When de-serialized it return the Javascript object. 反序列化时,它返回Javascript对象。

I did some Googling but can't find the how they are related? 我做了一些谷歌搜索,但是找不到它们之间的关系吗? They are often mentioned together. 它们经常一起被提及。 I need to know what is going on under the hood when we say that we need to serialize the data in JSON . 当我们说我们需要序列化JSON的数据时,我需要知道到底发生了什么

JSON is basically structured text. JSON基本上是结构化文本。

One example would be a client-server architecture, such as web browser - server communication is. 一个示例是客户端-服务器体系结构,例如Web浏览器-服务器通信。 The data from the server can be serialized from native format to JSON (text) and transferred through the network (stream of bytes) to the client (web browser). 来自服务器的数据可以从本机格式序列化为JSON(文本),并通过网络(字节流)传输到客户端(Web浏览器)。 When the data gets to the client (web browser), it is deserialized to a Javascript object and handled (maybe something is displayed to the user or the UI manipulated - non-essential). 当数据到达客户端(Web浏览器)时,将其反序列化为Javascript对象并进行处理(也许某些内容会显示给用户或操作的UI-非必需)。

A Java example 一个Java示例

Let's say we've a simple class in our server application, which is written in Java: 假设我们在服务器应用程序中有一个简单的类,它是用Java编写的:

class Person {
    String name;
    int age;
}

and we've an instance of this class: 我们有一个此类的实例:

Person person = new Person();
person.name = "John";
person.age = 32;

We've chosen to use JSON as the data format in server-to-client communications. 我们选择在服务器到客户端的通信中使用JSON作为数据格式。 Converting our instance of a person to JSON looks like this: 将我们的人员实例转换为JSON看起来像这样:

String json = "{\"person\": {\"name\": \"John\", \"age\": 32} }";

Now we just write this String to HTTP response, from where the client reads it - to a string. 现在,我们将这个String写入HTTP响应,从客户端读取它的位置-一个字符串。 The string is then deserialized to a Javascript object. 然后将字符串反序列化为Javascript对象。 Deserialization could look like this (more about JSON.parse at MDN / JSON.parse ): 反序列化可能看起来像这样(有关MDN / JSON.parse的JSON.parse的更多信息):

/* We get responseText by AJAX and it looks exactly the same that we sent from our web
 server - IT IS TEXT, the variable 'json' contents in our Java application. */
var responseText = "..."
var data = JSON.parse( responseText ); // JSON.parse is supported by modern browsers

Data looks something like this if written directly as a Javascript object: 如果直接将其写为Javascript对象,则数据如下所示:

data = {
    person: {
        name: john,
        age: 32
    }
};

So, we started in the server side from a Java-object. 因此,我们从Java对象开始在服务器端。 We serialized the Java-object to a JSON-string and sent it through the network to the client. 我们将Java对象序列化为JSON字符串,然后通过网络将其发送到客户端。 The client deserialized the JSON-string to a Javascript-object (which can be properly dealt with in language-specific way). 客户端将JSON字符串反序列化为Javascript对象(可以通过特定于语言的方式正确处理)。

JSON is a format that allows you to represent your objects as a string / text, which can then be stored, sent etc. JSON是一种格式,允许您将对象表示为字符串/文本,然后可以将其存储,发送等。

Another example would be XML, which is also often used for serialization. 另一个示例是XML,它也经常用于序列化。

Example: 例:

Object 宾语

var Person = new Object();
Person.FirstName = "Foo";
Person.LastName = "Foo";
Person.Age = 24;

JSON example serialization JSON示例序列化

    {
        Person:
        {
             FirstName: "Foo",
             LastName: "Bar",
             Age: 42
        }
    }

XML example serialization XML示例序列化

    <Person>
        <FirstName>Foo</FirstName>
        <LastName>Bar</LastName>
        <Age>42</Age>
    </Person>

Since you asked "what is going on under the hood": Simply explained, the serialization algorithm would iterate all properties of the object and store their names and values in a string of the requested format (JSON, XML, ...) 由于您询问“幕后情况”:简单地说,序列化算法将迭代对象的所有属性,并将其名称和值存储在所请求格式的字符串(JSON,XML等)中。

JSON represent collections of key/value pairs. JSON表示键/值对的集合。 JSON Parsers (like JSON.parse , json_decode ) will map this representation to an appropriate data structure that reflects these collections. JSON解析器(如JSON.parsejson_decode )会将此表示形式映射到反映这些集合的适当数据结构。 JSON Serializers (like JSON.stringify , json_encode ) do the inverse, they take a data structure and map it to JSON. JSON序列化器(例如JSON.stringifyjson_encode )进行相反的处理,它们采用数据结构并将其映射到JSON。

For example in PHP: 例如在PHP中:

$obj['a'] = 'b'; //Associative Array
$obj['c'] = 'd';

Its JSON representation would be: 其JSON表示为:

'{
    "a" : "b",
    "c" : "d"
}'

Keys and values of the respective data structure (in this case an associative array). 各个数据结构的键和值(在这种情况下为关联数组)。

In Javascript: 用Javascript:

var obj = {
    a : 'b',
    c : 'd'
}

This is a Javascript object, when serialized it maps to the same JSON: 这是一个Javascript对象,在序列化时会映射到相同的JSON:

'{
    "a" : "b",
    "c" : "d"
}'

JSON describes a precise string format, which is why I've marked my JSON in between ' ' . JSON描述了一种精确的字符串格式,这就是为什么我将JSON标记在' '之间。 A string representation of your data structures can be very useful for many things, like sending them as a message. 数据结构的字符串表示形式对许多事情都非常有用,例如将其作为消息发送。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM