I am using Newtonsoft to parse some JSon into a .Net type. The json contains an array of arrays called 'data'. I would like to make each array within the data array it's own type, but am unsure how to do this.
Hopefully the code below demonstrates this.
public class TheData
{
[JsonProperty(PropertyName = "data")]
public List<object> dataItems { get; set; }
}
Usage:
string json =
"{\"data\":[[\"20180511\",1094391],[\"20180504\",1097315],[\"20180427\",1100221],[\"20180420\",1094455],[\"20180413\",1093023]]}";
var myObj = JsonConvert.DeserializeObject<TheData>(json);
This works ok, however, I would like to change the type of dataItems from List to List as below:
public class TheData
{
[JsonProperty(PropertyName = "data")]
public List<DataItem> dataItems { get; set; }
}
public class DataItem
{
public string deldate { get; set; }
public int value { get; set; }
}
However, this results in an exception:
Newtonsoft.Json.JsonSerializationException occurred
HResult=-2146233088
Message=Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'CE.FOTools.Feeds2.EIA.DataItem' 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<T> 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 'data[0]', line 1, position 10.
The error message suggests my desired outcome may not be possible, but can anyone suggest how to correct this? I have no control over the JSON format (unles I operate on the string once it is retrieved). I'm using .Net 4.5 if that makes any difference.