# .NET and More > WPF, WCF, WF >  Problem Deserializing the jsonString

## kumika

I have a question that requires a bird's eye view of this topic, since I am a newbie on WCF. I retrieved data from the database and serialized it into a valid json string. I set the service-side object properties using the <DataContract()> and the <Datamember()> tag as required. However when I tried to deserialized the json string I always get the Error: The data contract type '...' cannot be deserialized because the required data members '...' were not found. The string I used to deserialize is: [Dim strm As New MemoryStream(Encoding.UTF8.GetBytes(json))
Dim ser As New _
DataContractJsonSeriaalizer(ldsp.[GetType]())
ldsp = DirectCast(ser.ReadObject(strm), List (Of lds))]

Ldsp is: Dim ldsp as New List (Of lds)), where lds is a proxy object I.e. Client-side object, whicj has a <Serializable()> tag on top of the corresponding property names or public variables, as required. 

My questions are: 1. What causes this error? 
2. Do I need to add values or initialize the service object before doing the deserialization or do I leave the object empty since all the data is in the jsonstring? 
3. Please refer me to a good tutorial on how to deserialize the object properties, with the List (Of Object). 
4. I suspect the problem has to do with the : Return list (Of lds) but I can't see how. Is this Return statement correct. 

Please assist. I have tried the net with no luck and am desperate now. Thanks

----------


## NeedSomeAnswers

Have you included the same class with your DataContract and DataMembers in it, in your Client Side project ??

To DeSerialise your data it needs the exact same class and properties to deserialise into.

So my json deserialise calls look like this - 



```
List<User> userList = Json.DeserialiseJSon<List<User>>(json);
```


Also a tip, dont use the built in .Net Json Serialisation stuff its not very nice to use, the NewtonSoft json stuff is much nicer to use i would recommend it. 

You can just download it through NuGet. 

Using NewtonSoft Json i created a class like this



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

namespace BusinessObjects
{
    public class Json
    {
        public static T DeserialiseJSon<T>(string json)
        {
            T obj = (T)JsonConvert.DeserializeObject<T>(json);
            return obj;
        }

        public static object DeserialiseJSon(string json)
        {
            Object obj = JsonConvert.DeserializeObject(json);
            return obj;
        }
    }
}
```

And then you can just do 1 line calls like the call i mention above or this one - 



```
User user = Json.DeserialiseJSon<User>(json);
```

If you just want to deserialise an object rather than a list of objects

----------


## kumika

Here is my complete code. Can you please look through and see what I am doing wrong. I am still trying to get my head around this. Thanks you.



[<CLIENT-SIDE CODE AND CLASS
'"""""""""""""""
Class MainWindow

    Dim pType As String
    Dim jsonString As String
    Dim sdl As New List(Of sampleData)

    '"""""""""""""""""""""""""""""""""""""""""""
    Private WithEvents service As New WebClient()     

'""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click

        pType = "findData"

        '"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
        If Not service.IsBusy Then

            service.DownloadStringAsync(New Uri( _
                                        "http://localhost:49206/JsonExample/" _
                                        & "Service.svc/sampleData/" & pType))
        Else

            MessageBox.Show("The web-service is still busy. Please wait.", "Service Busy", MessageBoxButton.OK, MessageBoxImage.Hand)

        End If

    End Sub

    '""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
    Private Sub service_DownloadStringCompleted(ByVal sender As Object, _
                                                ByVal e As System.Net.DownloadStringCompletedEventArgs) _
                                                Handles service.DownloadStringCompleted

        '"""""""""""""""""""""""""""""""""""""""""""""""""""""""
             jsonString =  TextBox1.Text

                Dim strm As New MemoryStream(Encoding.UTF8.GetBytes(jsonString))     
                Dim JSONSerialize3 As New  _
                 DataContractJsonSerializer(sdl.[GetType]())
                sdl = DirectCast(JSONSerialize3.ReadObject(strm), List(Of sampleData))

                strm.Close()
                strm.Dispose()
        End If
    End Sub
End Class


 CLIENT  SIDE CLASS  

<Serializable()> _
Public Class sampleData

    Public ptValue As String
 Public nameValue As String
    Public idValue As String
 Public jsonValue As String

End Class

 CLIENT  SIDE CLASS  


'""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
SERVICE-SIDE CODE

Public Class JsonExample

    Implements IJsonExample

    '""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
Function getSampleData(ByVal ptype As String) As System.Collections.Generic.List(Of sampleData) _
        Implements IJsonExample.getSampleData

        getSampleData = New System.Collections.Generic.List(Of sampleData)()
        '"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
Dim JSerializer As New System.Web.Script.Serialization.JavaScriptSerializer
Dim rowsList As New System.Collections.Generic.List(Of Dictionary(Of String, Object))()

   Dim row As Dictionary(Of String, Object)

GENERATE SAMPLE DATA AND SERIALIZE IT
        '"""""""""""""""""""""""""""""""""""
        For i = 0 To 1
            row = New Dictionary(Of String, Object)()

            For ii = 0 To 1
                row.Add("Column " & ii + 1, i + ii + 20)
                sampleData.name = "Column " & ii + 1
                sampleData.id = i + ii + 20
            Next

            '""""""""""""""""""""""""""
            rowsList.Add(row)

        Next

'""""""""""""""""""""""""""""""""""""""""""""""""
        sampleData.json = JSerializer.Serialize(rowsList)

        '"""""""""""""""""""""""""""""
        Return getSampleData

    End Function             'end of lessonDetails

End Class
'""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
RESULT STRING : [{"Column 1":20,"Column 2":21},{"Column 1":21,"Column 2":22}]
'""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

Imports System.ServiceModel.Web

<ServiceContract()> _
Public Interface IJsonExample

    '"""""""""""""""""""""""""""""""""""""""""""""
    <OperationContract()> _
    <WebGet(ResponseFormat:=WebMessageFormat.Json, _
           UriTemplate:="sampleData/{ptype}")> _
   Function getSampleData(ByVal ptype As String) As System.Collections.Generic.List(Of sampleData)

End Interface  
    '""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

   SERVICE  SIDE CLASS 
Public Class sampleData

    Public Shared id As String
    Public Shared name As String
    Public Shared json As String
    Dim pt As String

    '""""""""""""""""""""""""""""
    Public Sub New()

        MyClass.New("")

    End Sub              

    '""""""""""""""""""""""""""""""""""""""""""""""""""
    Public Sub New(ByVal ptValue As String)

        pt = ptValue
        name = nameValue
        id = idValue
        json = jsonValue

    End Sub

    '""""""""""""""""""""""""""""""""""""""""""""""""""
    <DataMember()> _
    Public Property nameValue() As String
        Get
            Return name
        End Get

        Set(ByVal value As String)
            name = value
        End Set
    End Property       'Operation

        <DataMember()> _
    Public Property idValue() As String
        Get
            Return id
        End Get

        Set(ByVal value As String)
            id = value
        End Set
    End Property       'Operation

    <DataMember()> _
    Public Property jsonValue() As String
        Get
            Return json
        End Get

        Set(ByVal value As String)
            json = value
        End Set
    End Property       'Operation

End Class
>]







> Have you included the same class with your DataContract and DataMembers in it, in your Client Side project ??
> 
> To DeSerialise your data it needs the exact same class and properties to deserialise into.
> 
> So my json deserialise calls look like this - 
> 
> 
> 
> ```
> ...

----------


## techgnome

And I'll repeat "To DeSerialise your data it needs the exact same class and properties to deserialise into." ... the same, as in the EXACT SAME class... not two classes that look the same, but the actual honest to goodness SAME CLASS with the exact same fully qualified name...   The class on the server isn't the same as the one on your client side... they're different.

Normally in these cases, the solution is to put the class into a third assembly, then it can be referenced by each of the other assemblies, and the result is the same FQN on both ends,  allowing for serialization and deserialization.

-tg

----------


## kumika

I have one foot in there. Thanks a lot guys. Just one last hurdle, I hope. When testing for data using: 
<Code> Msgbox(sdl.count & " : " & sdl.Item(0).nameValue &  sdl.Item(0).idValue)</Code>

I get 2 for counts, which is correct,  but there are no items. Can you see what I am missing or is the format of the jsonstring incorrect? Thanks again.




> And I'll repeat "To DeSerialise your data it needs the exact same class and properties to deserialise into." ... the same, as in the EXACT SAME class... not two classes that look the same, but the actual honest to goodness SAME CLASS with the exact same fully qualified name...   The class on the server isn't the same as the one on your client side... they're different.
> 
> Normally in these cases, the solution is to put the class into a third assembly, then it can be referenced by each of the other assemblies, and the result is the same FQN on both ends,  allowing for serialization and deserialization.
> 
> -tg

----------


## NeedSomeAnswers

So sdl.count is 2 but what is sdl.Item.Count ? 

The point being surely you should be either doing - 



```
sdl.Item.count & sdl.Item(0).nameValue
```

or



```
sdl.Count & sdl(0).nameValue
```

----------


## kumika

Guys thanks a lot. I have managed to crack it now. The problem was that, since the data came from a database, the class properties and the data fields were not matching. But I thank you for guiding me through. A few points to make, though: 
1. The code I posted works fine if working with an object. I fails when working with a list of objects. 
2. The server-side class is necessary only if you are going to use it to generate data. Otherwise it is not necessary. For example, my data source was a database so the system works even I deleted it or it does not match the client-side class. 
3. The properties or fields in the data source i.e. object or database, must be identical to the properties in the clientt-side object. In fact, as tg said, the two classes must be identical in all respects. Otherwise, deserialization will fail. 

I ask the experts to edit my observations, if necessary. Thanks.




> So sdl.count is 2 but what is sdl.Item.Count ? 
> 
> The point being surely you should be either doing - 
> 
> 
> 
> ```
> sdl.Item.count & sdl.Item(0).nameValue
> ```
> ...

----------


## NeedSomeAnswers

> 1. The code I posted works fine if working with an object. I fails when working with a list of objects.


That is because in order to deserialise into a list you need to have a serialised list in the first place, i suspect if you are grabbing data directly from a database you don't have a list of objects in the right format. 

If loaded your data on the server side into a list of objects and then serialise that, you would then be able to deserialise it into a list in your client.

----------


## kumika

Yes, after grabbing data from the database, I first serialized it into the json string and the transferred it to the client for deserializarion. Data can't just be deserialized if not serialized.




> That is because in order to deserialise into a list you need to have a serialised list in the first place, i suspect if you are grabbing data directly from a database you don't have a list of objects in the right format. 
> 
> If loaded your data on the server side into a list of objects and then serialise that, you would then be able to deserialise it into a list in your client.

----------


## NeedSomeAnswers

> Yes, after grabbing data from the database, I first serialized it into the json string and the transferred it to the client for deserializarion. Data can't just be deserialized if not serialized.


Err yes, but that's not what i was saying !!!

I was trying to explain why *you could NOT deserialise into a LIST* using your current method and what you would need to do change that!!!

----------

