I am deserializing JSON using Json.NET. How can I ignore a blank array that unexpectedly occurs inside an array of objects during deserialization?
I have tested the following JSON from a third party on this website http://json.parser.online.fr/ which confirm that it is well-formed:
{
"total_events": 3551574,
"json.appID": [
{
"count": 3551024,
"term": 1
},
{
"count": 256,
"term": 2
},
[] /* <----- I need to ignore this empty array */
],
"unique_field_count": 2
}
I would like to deserialize it to the following model:
public class RootObject
{
[JsonProperty("total_events")]
public int TotalEvents { get; set; }
[JsonProperty("json.appID")]
public List<JsonAppID> AppIds { get; set; }
[JsonProperty("unique_field_count")]
public int UniqueFieldCount { get; set; }
}
public class JsonAppID
{
[JsonProperty(PropertyName = "count")]
public int Count { get; set; }
[JsonProperty(PropertyName = "term")]
public string Term { get; set; }
}
But when I try, I get the following exception:
Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'JsonAppID' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.
Path '['json.appID'][2]', line 12, position 6.
How can I ignore the unneeded empty array inside the "json.appID"
array and successfully deserialize my model?