错误:预期状态。无法反序列化 JSON 对象

Posted

技术标签:

【中文标题】错误:预期状态。无法反序列化 JSON 对象【英文标题】:Error: Expecting state. Could not Deserialize JSON Object 【发布时间】:2011-01-20 02:23:31 【问题描述】:

我们正在尝试使用以 JSON 格式返回员工详细信息的 WCF 服务。 喜欢:


  "d": [
    "__type": "Employee:#",
    "BigThumbNailURI": null,
    "ID": 1,
    "Name": "E1"
  , 
    "__type": "Employee:#",
    "BigThumbNailURI": null,
    "ID": 2,
    "Name": "E1"
  ]

当我试图反序列化它时,从后面的 VB.net 代码中可以看出

“期待状态'元素'..遇到名称'',命名空间''的'文本'。”

反序列化代码sn-p:

Dim serializer = New DataContractJsonSerializer(GetType(List(Of Employee)))
Dim memoryStream = New MemoryStream()
Dim s = msg.Content.ReadAsString()
serializer.WriteObject(memoryStream, s)
memoryStream.Position = 0

' Code for Deserilization

Dim obj As List(Of Employee) = serializer.ReadObject(memoryStream)
memoryStream.Close()


'Employee Class

<DataContract()> _
Public Class Employee

    Private _Name As String
    <DataMember()> _
    Public Property Name() As String
        Get
            Return _Name
        End Get
        Set(ByVal value As String)
            _Name = value
        End Set
    End Property


    Private _id As Integer
    <DataMember()> _
    Public Property ID() As Integer

        Get
            Return _id
        End Get
        Set(ByVal value As Integer)
            _id = value
        End Set
    End Property


End Class

有人遇到过这个问题吗?

【问题讨论】:

【参考方案1】:

找到解决方案。要解决此问题,请不要再使用该 MemoryStream。 将 JSON 对象直接传递给反序列化器,如下:

Dim serializer = New DataContractJsonSerializer(GetType(List(Of Employee))) 

' Code for Deserilization 

Dim obj As List(Of Employee) = serializer.ReadObject(msg.Content.ReadAsString()) 
memoryStream.Close()

【讨论】:

【参考方案2】:

这里有两种通用的序列化和反序列化方法。

        /// <summary>
        /// Serializes the specified object into json notation.
        /// </summary>
        /// <typeparam name="T">Type of the object to be serialized.</typeparam>
        /// <param name="obj">The object to be serialized.</param>
        /// <returns>The serialized object as a json string.</returns>
        public static string Serialize<T>(T obj)
        
            Utils.ArgumentValidation.EnsureNotNull(obj, "obj");

            string retVal;
            using (MemoryStream ms = new MemoryStream())
            
                DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
                serializer.WriteObject(ms, obj);
                retVal = Encoding.UTF8.GetString(ms.ToArray());
            

            return retVal;
        

        /// <summary>
        /// Deserializes the specified json string into object of type T.
        /// </summary>
        /// <typeparam name="T">Type of the object to be returned.</typeparam>
        /// <param name="json">The json string of the object.</param>
        /// <returns>The deserialized object from the json string.</returns>
        public static T Deserialize<T>(string json)
        
            Utils.ArgumentValidation.EnsureNotNull(json, "json");

            T obj = Activator.CreateInstance<T>();
            using (MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(json)))
            
                DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
                obj = (T)serializer.ReadObject(ms);
            

            return obj;
        

您将需要这些命名空间

using System;
using System.IO;
using System.Runtime.Serialization.Json;
using System.Text;

【讨论】:

以上是关于错误:预期状态。无法反序列化 JSON 对象的主要内容,如果未能解决你的问题,请参考以下文章

Json.JsonSerializationException : 无法反序列化当前 JSON 对象

Blazor WASM“无法反序列化当前 JSON 对象(例如 "name":"value")”错误

无法反序列化当前的JSON对象,为啥

反序列化 Json 出现错误“无法反序列化当前 JSON 数组”

无法反序列化 JSON 响应

在 C# 中反序列化复杂对象