user8393563
user8393563

Reputation:

App crashes when deserializing JSON array of objects without name

I am currently working on a ListView which is showing several processes of our system. The system unfortunatedly only provide the json in the following format:

[
    {
        "f1": "w1",
        "date": "2018.11.09"        
    },
    {
        "f1": "w2",
        "date": "2018.11.09"
    }
]

My app crashes if it starts the deserialization and I think the reason is, that the object has no "description". But how do I solve this?

this is my code for downloading and deserialize the json:

using (WebClient client = new WebClient())
{
    client.Encoding = Encoding.UTF8;                       
    json = client.DownloadString("http://x.x.x.x/test.json");
}
return JsonConvert.DeserializeObject<Query>(json);

Query:

public class Query
{
    public List<Process> Processes { get; set; }
}

Process:

public class Process
{
    [JsonProperty("f1")]
    public string f1 { get; set; }
    [JsonProperty("date")]
    public string date { get; set; }
}

Upvotes: 0

Views: 554

Answers (1)

Tom Johnson
Tom Johnson

Reputation: 699

This is because JSON is trying to deserialise your values into a 'Query' object as specified:

{
    "processes" : []
}

When the result is a List, change the JsonConvert.DeserializeObject<Query> to JsonConvert.DeserializeObject<List<Process>>, and see if that helps :).

Upvotes: 1

Related Questions