共用方式為


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

要序列化的物件。

傳回

String

序列化的 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 字串的名稱/值組字典。

如果物件圖形太複雜,或 已註冊的 JavaScriptConverter 實例造成轉換器遞迴,方法 Serialize 也可以擲回例外狀況。

適用於

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

StringBuilder 物件,用來撰寫 JSON 字串。

例外狀況

產生的 JSON 字串超過 MaxJsonLength 的值。

-或- obj 包含循環參考。 當子物件具有父物件的參考,而父物件又具有子物件的參考時,就會發生循環參考。

已超出 RecursionLimit 所定義的遞迴限制。

outputnull

備註

JavaScriptSerializer當實例將註冊自訂轉換器的類型序列化時,序列化程式會呼叫 Serialize 方法,以取得將轉換成 JSON 字串的名稱/值組字典。

如果物件圖形太複雜,或 已註冊的 JavaScriptConverter 實例造成轉換器遞迴,方法 Serialize 也可以擲回例外狀況。

適用於