0

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."

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.