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

Asp.net 错误:预期状态。无法反序列化JSON对象,asp.net,vb.net,wcf,json,serialization,Asp.net,Vb.net,Wcf,Json,Serialization,我们正在尝试使用WCF服务,该服务以JSON格式返回员工详细信息。 比如: 当我试图反序列化它时,从VB.net代码中可以看出 “预期状态为“元素”…遇到名称为“”、命名空间为“”的“文本”。 反序列化代码段: Dim serializer = New DataContractJsonSerializer(GetType(List(Of Employee))) Dim memoryStream = New MemoryStream() Dim s = msg.Content.ReadAsStri

我们正在尝试使用WCF服务,该服务以JSON格式返回员工详细信息。 比如:

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

“预期状态为“元素”…遇到名称为“”、命名空间为“”的“文本”。 反序列化代码段:

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
Dim serializer=newdatacontractjsonserializer(GetType(员工列表)))
Dim memoryStream=新的memoryStream()
Dim s=msg.Content.ReadAsString()
serializer.WriteObject(memoryStream,s)
memoryStream.Position=0
'反序列化代码
Dim obj As List(员工的)=serializer.ReadObject(memoryStream)
memoryStream.Close()
“雇员阶级
_
公营雇员
Private\u名称作为字符串
_
作为字符串的公共属性名()
得到
返回\u名称
结束
设置(ByVal值作为字符串)
_名称=值
端集
端属性
私有_id为整数
_
公共属性ID()为整数
得到
返回id
结束
设置(ByVal值为整数)
_id=值
端集
端属性
末级


有人遇到过这个问题吗?

找到了解决方案。要解决此问题,请不要再使用该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()

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

        /// <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;
        }
        /// <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;