JavaScriptSerializer.Serialize 方法

定义

将对象转换为 JSON 字符串。

重载

Serialize(Object)

将对象转换为 JSON 字符串。

Serialize(Object, StringBuilder)

序列化对象并将生成的 JSON 字符串写入指定的 StringBuilder 对象。

Serialize(Object)

将对象转换为 JSON 字符串。

public:
 System::String ^ Serialize(System::Object ^ obj);
public string Serialize (object obj);
member this.Serialize : obj -> string
Public Function Serialize (obj As Object) As String

参数

obj
Object

要序列化的对象。

返回

序列化的 JSON 字符串。

例外

所生成的 JSON 字符串超出了 MaxJsonLength 的值。

- 或 -

obj 包含循环引用。 循环引用出现在子对象引用父对象,而父对象又引用子对象时。

超出了由 RecursionLimit 定义的递归限制。

示例

以下示例提供了一个简单的说明,说明如何序列化和反序列化数据对象。

using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.Script.Serialization;

namespace ExampleApplication
{
    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var RegisteredUsers = new List<Person>();
            RegisteredUsers.Add(new Person() { PersonID = 1, Name = "Bryon Hetrick", Registered = true });
            RegisteredUsers.Add(new Person() { PersonID = 2, Name = "Nicole Wilcox", Registered = true });
            RegisteredUsers.Add(new Person() { PersonID = 3, Name = "Adrian Martinson", Registered = false });
            RegisteredUsers.Add(new Person() { PersonID = 4, Name = "Nora Osborn", Registered = false });

            var serializer = new JavaScriptSerializer();
            var serializedResult = serializer.Serialize(RegisteredUsers);
            // Produces string value of:
            // [
            //     {"PersonID":1,"Name":"Bryon Hetrick","Registered":true},
            //     {"PersonID":2,"Name":"Nicole Wilcox","Registered":true},
            //     {"PersonID":3,"Name":"Adrian Martinson","Registered":false},
            //     {"PersonID":4,"Name":"Nora Osborn","Registered":false}
            // ]

            var deserializedResult = serializer.Deserialize<List<Person>>(serializedResult);
            // Produces List with 4 Person objects
        }
    }
}
Imports System.Web.Script.Serialization

Public Class _Default
    Inherits Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        Dim RegisteredUsers As New List(Of Person)()
        RegisteredUsers.Add(New Person With {.PersonID = 1, .Name = "Bryon Hetrick", .Registered = True})
        RegisteredUsers.Add(New Person With {.PersonID = 2, .Name = "Nicole Wilcox", .Registered = True})
        RegisteredUsers.Add(New Person With {.PersonID = 3, .Name = "Adrian Martinson", .Registered = False})
        RegisteredUsers.Add(New Person With {.PersonID = 4, .Name = "Nora Osborn", .Registered = False})

        Dim serializer As New JavaScriptSerializer()
        Dim serializedResult = serializer.Serialize(RegisteredUsers)
        ' Produces string value of:
        ' [
        '     {"PersonID":1,"Name":"Bryon Hetrick","Registered":true},
        '     {"PersonID":2,"Name":"Nicole Wilcox","Registered":true},
        '     {"PersonID":3,"Name":"Adrian Martinson","Registered":false},
        '     {"PersonID":4,"Name":"Nora Osborn","Registered":false}
        ' ]

        Dim deserializedResult = serializer.Deserialize(Of List(Of Person))(serializedResult)
        ' Produces List with 4 Person objects
    End Sub
End Class

它需要名为 Person的类,如以下示例所示。

namespace ExampleApplication
{
    public class Person
    {
        public int PersonID { get; set; }
        public string Name { get; set; }
        public bool Registered { get; set; }
    }
}
Public Class Person
    Public Property PersonID As Integer
    Public Property Name As String
    Public Property Registered As Boolean
End Class

注解

JavaScriptSerializer当实例序列化为其注册了自定义转换器的类型时,序列化程序将调用 Serialize 方法以获取将转换为 JSON 字符串的名称/值对字典。

Serialize如果对象图太复杂,或者 已注册的JavaScriptConverter实例导致转换器递归,则 方法也可能引发异常。

适用于

Serialize(Object, StringBuilder)

序列化对象并将生成的 JSON 字符串写入指定的 StringBuilder 对象。

public:
 void Serialize(System::Object ^ obj, System::Text::StringBuilder ^ output);
public void Serialize (object obj, System.Text.StringBuilder output);
member this.Serialize : obj * System.Text.StringBuilder -> unit
Public Sub Serialize (obj As Object, output As StringBuilder)

参数

obj
Object

要序列化的对象。

output
StringBuilder

用于写入 JSON 字符串的 StringBuilder 对象。

例外

所生成的 JSON 字符串超出了 MaxJsonLength 的值。

- 或 -

obj 包含循环引用。 循环引用出现在子对象引用父对象,而父对象又引用子对象时。

超出了由 RecursionLimit 定义的递归限制。

outputnull

注解

JavaScriptSerializer当实例序列化为其注册了自定义转换器的类型时,序列化程序将调用 Serialize 方法以获取将转换为 JSON 字符串的名称/值对字典。

Serialize如果对象图太复杂,或者 已注册的JavaScriptConverter实例导致转换器递归,则 方法也可能引发异常。

适用于