Deserialize nested JSON array in C#

别说谁变了你拦得住时间么 提交于 2019-12-01 23:46:25

Your root object is a 2d jagged array of objects. The properties "children" are also 2d jagged arrays. Thus your Menu class needs to be:

public class Menu
{
    public int id { get; set; }
    public string name { get; set; }
    public Menu [][] children { get; set; }
}

And deserialize your JSON as follows:

var serializer = new JavaScriptSerializer();
var objects = serializer.Deserialize<Menu [][]>(jsonData);

Alternatively, if you prefer lists to arrays, do:

public class Menu
{
    public int id { get; set; }
    public string name { get; set; }
    public List<List<Menu>> children { get; set; }
}

And then

var objects = serializer.Deserialize<List<List<Menu>>>(jsonData);

Could the issue be that the actual data is an array but you're telling it to expect just one Menu?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!