ruedi
ruedi

Reputation: 5545

Problems with Json deserialisation within .NET

I build the following classes where I want to store the data (help can also be in c#)

Public Class DatasourceInfos
    Public Property DSContainer() As List(Of DatasourceInfo)
End Class

Public Class DatasourceInfo
    Public Property name As String
    Public Property description As String
    Public Property created As String
    Public Property modified As String
    Public Property identifier As String
    Public Property href As String
    Public Property cacheAvailable As Boolean
    Public Property id As ULong
End Class

I receive data in json format as follows:

[{  "name":"MyName1",
    "description":"MyDescription1",
    "created":1244193265000,
    "modified":1264515442000,
    "identifier":"Identifier==",
    "href":"https://....",
    "cacheAvailable":true,
    "id":29},

    {"name":"MyName2",
    "description":"MyDescription2",
    "created":1244193265000,
    "modified":1264515442000,
    "identifier":"Identifier==",
    "href":"https://....",
    "cacheAvailable":true,
    "id":30}]

Using RestSharp (solution must not use RestSharp) I try to get the data into my class with:

Public Function getDatasources(ByVal _token As String) As String
    Dim client = New RestClient(_baseURI)
    Dim request = New RestRequest("/data", Method.GET)
    request.AddHeader("Authorization", "Basic " + _token)
    Dim response = client.Execute(Of DatasourceInfos)(request)
    Return response
End Function

But looking into the response object there is nothing mapped to my classes. Anyone who can tell me what I am doing wrong? I compared this code with many others on SO but I just cannot see, what is wrong.

Upvotes: 1

Views: 87

Answers (1)

In the json shown, there isnt anything relating to your DatasourceInfos container class. For that, the json would look something like:

{
    "DSInfoItems":
     ...all your json ....
}

JSON.NET, at least, barks about the format when trying to deserialize to DatasourceInfos but will deserialize to an array or list of DatasourceInfo easily:

Dim DS = JsonConvert.DeserializeObject(Of List(Of DataSource))(jstr)

Upvotes: 1

Related Questions