Reputation: 3218
This is similar to other postings, but my variation is that my json object begins as an array of arrays, and I can't get it to deserialize.
class Program
{
static void Main(string[] args)
{
var json = @"[[{""f1"":1, ""f2"":2}]]";
var obj = JsonConvert.DeserializeObject<RootObject[]>(json);
}
}
public class RootObject
{
public List<InnerObject> InnerObjects { get; set; }
}
public class InnerObject
{
public int f1 { get; set; }
public int f2 { get; set; }
}
I've also tried
JsonConvert.DeserializeObject<List<RootObject>>(json);
and various other variations. The exception text begins "Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'jsontest.RootObject' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly."
Upvotes: 0
Views: 670
Reputation: 129667
Your JSON corresponds to a List<List<InnerObject>>
not a RootObject[]
. (For the latter, the JSON would need to look like this: [{ "InnerObjects" : [{"f1": 1, "f2": 2}] }]
).
Try deserializing like this:
var list = JsonConvert.DeserializeObject<List<List<InnerObject>>>(json);
Fiddle: https://dotnetfiddle.net/ELnmfg
Upvotes: 2