0

I have one class defined as

public class TestRun
{
    [JsonProperty("links")]
    public List<Links> Links { get; set; }
    [JsonProperty("id")]
    public string id { get; set; }
    [JsonProperty("name")]
    public string name { get; set; }
    [JsonProperty("pid")]
    public string pid { get; set; }
    [JsonProperty("properties")]
    public List<Properties> Properties { get; set; }
    [JsonProperty("test_case")]
    public TestRunCase test_case { get; set; }
    [JsonProperty("test_case_version_id")]
    public string test_case_version_id { get; set; }
    [JsonProperty("page")]
    public string page { get; set; }
    [JsonProperty("page_size")]
    public string page_size { get; set; }
    [JsonProperty("total")]
    public string total { get; set; }
    [JsonProperty("items")]
    public List<Items> Items { get; set; }
}

Another part of my code fetches a JSON data string and assigned the converted data to an array

TestRun[] RunData = JsonConvert.DeserializeObject<TestRun[]>(testrun_Data);

Most of time this JSON string of testrun_Data consists of an array of TestRun type of data which is fine. But when it is like what's shown below,

{"links":[],"page":1,"page_size":100,"total":0,"items":[]}

An exception of type Newtonsoft.Json.JsonSerializationException will be thrown.

Brian Rogers
  • 125,747
  • 31
  • 299
  • 300
  • Have you tried adding a constructor method to your TestRun class that initializes the properties of type List to a new List? I think what is happening has to do with "links" and "items" in your JSON string being empty arrays. – RobJarvis Jul 29 '20 at 19:07
  • 1
    Show us the JSON that works. – Robert Harvey Jul 29 '20 at 19:09
  • If your JSON sometimes consists of an array of objects, and sometimes consists of a single object, you can deserialize to a `List` by using `SingleOrArrayConverter` from [How to handle both a single item and an array for the same property using JSON.net](https://stackoverflow.com/q/18994685/3744182). (The converters there are specifically for `List` so if you need an array you will need to do `.ToArray()` afterwards.) – dbc Jul 30 '20 at 18:07
  • Demo using `SingleOrArrayListConverter` from [How to handle both a single item and an array for the same property using JSON.net](https://stackoverflow.com/q/18994685/3744182) here: https://dotnetfiddle.net/m19Q4I – dbc Jul 30 '20 at 18:14

3 Answers3

1

Your provided json string contains an json object, not json array, so you should deserialize it accordingly:

TestRun RunData = JsonConvert.DeserializeObject<TestRun>(testrun_Data);
Guru Stron
  • 102,774
  • 10
  • 95
  • 132
1

Your JSON looks like this:

{"links":[],"page":1,"page_size":100,"total":0,"items":[]}

To deserialize it to an array, it would need to look like this:

[{"links":[],"page":1,"page_size":100,"total":0,"items":[]}]
Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
0

You can use the https://json2csharp.com tool to find the object where the json can be deserialized.

G.Mich
  • 1,607
  • 2
  • 24
  • 42