Επεξεργασία

Κοινή χρήση μέσω


JavaScriptSerializer.Deserialize Method

Definition

Overloads

Deserialize(String, Type)

Converts a JSON-formatted string to an object of the specified type.

Deserialize<T>(String)

Converts the specified JSON string to an object of type T.

Deserialize(String, Type)

Converts a JSON-formatted string to an object of the specified type.

public:
 System::Object ^ Deserialize(System::String ^ input, Type ^ targetType);
public object Deserialize (string input, Type targetType);
member this.Deserialize : string * Type -> obj
Public Function Deserialize (input As String, targetType As Type) As Object

Parameters

input
String

The JSON string to deserialize.

targetType
Type

The type of the resulting object.

Returns

The deserialized object.

Exceptions

input is null.

The length of input exceeds the value of MaxJsonLength.

-or-

The recursion limit defined by RecursionLimit was exceeded.

-or-

input contains an unexpected character sequence.

-or-

input is a dictionary type and a non-string key value was encountered.

-or-

input includes member definitions that are not available on the target type.

input contains a "__type" property that indicates a custom type, but the type resolver that is currently associated with the serializer cannot find a corresponding managed type.

-or-

input contains a "__type" property that indicates a custom type, but the result of deserializing the corresponding JSON string cannot be assigned to the expected target type.

-or-

input contains a "__type" property that indicates either Object or a non-instantiable type (for example, an abstract type or an interface).

-or-

An attempt was made to convert a JSON array to an array-like managed type that is not supported for use as a JSON deserialization target.

-or-

It is not possible to convert input to the target type.

Remarks

This deserialization method does not try to cast the root of the object graph to a specific type, the way that the Deserialize method does.

Applies to

Deserialize<T>(String)

Converts the specified JSON string to an object of type T.

public:
generic <typename T>
 T Deserialize(System::String ^ input);
public T Deserialize<T> (string input);
member this.Deserialize : string -> 'T
Public Function Deserialize(Of T) (input As String) As T

Type Parameters

T

The type of the resulting object.

Parameters

input
String

The JSON string to be deserialized.

Returns

T

The deserialized object.

Exceptions

The input length exceeds the value of MaxJsonLength.

-or-

The recursion limit defined by RecursionLimit was exceeded.

-or-

input contains an unexpected character sequence.

-or-

input is a dictionary type and a non-string key value was encountered.

-or-

input includes member definitions that are not available on type T.

input is null.

input contains a "__type" property that indicates a custom type, but the type resolver associated with the serializer cannot find a corresponding managed type.

-or-

input contains a "__type" property that indicates a custom type, but the result of deserializing the corresponding JSON string cannot be assigned to the expected target type.

-or-

input contains a "__type" property that indicates either Object or a non-instantiable type (for example, an abstract types or an interface).

-or-

An attempt was made to convert a JSON array to an array-like managed type that is not supported for use as a JSON deserialization target.

-or-

It is not possible to convert input to T.

Examples

The following example provides a simple illustration of how to serialize and deserialize data objects.

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

It requires a class named Person, which is shown in the following example.

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

Remarks

The Deserialize method is equivalent to first using the DeserializeObject method to obtain an object graph and then trying to cast the result to type T.

During deserialization, the serializer's current type resolver is referenced, which determines the managed type to use when converting elements that are nested inside arrays and dictionary types. As a result, the deserialization process iterates through all nested elements of input. For more information about type resolvers, see the JavaScriptTypeResolver class.

Applies to