views:

452

answers:

2

We are trying to consume WCF service which returns employee details in JSON Format. like:

{"d":[{"_type":"Employee:#","BigThumbNailURI":null,"ID":1,"Name":"E1"},{"_type":"Employee:#","BigThumbNailURI":null,"ID":2,"Name":"E1"}]}

From VB.net code behind when I am trying to deserialize it it's stating that

"Expecting state 'Element'.. Encountered 'Text' with name '', namespace ''."

Deserialization code snippet:

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

Has anyone encountered this problem?

A: 

Found resolution. To solve this issue, do not use that MemoryStream anymore. Pass the JSON object to deserializer directly as follows:

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

' Code for Deserilization

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

ParagM
A: 

Here are two generic methods for serializing and deserializing.

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

You'll need these namespaces

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