How to read json file and serialize it in custom class? How to read json file and serialize it in custom class? json json

How to read json file and serialize it in custom class?


The serialization has an extra, unnamed container above menu. Your class structure needs to look like:

public class container{    public menu menu { get; set; }}public class menu{    public menuitem[] menuitems { get; set; }}public class menuitem{    public string Label { get; set; }    public string Listview { get; set; }}

And to deserialize, you can use:

JavaScriptSerializer js = new JavaScriptSerializer();StreamReader sr = new StreamReader("menu.json");string filedata = sr.ReadToEnd();var menus = js.Deserialize<container>(filedata);


I think you need to rename your Menu class to MenuItem:

public class MenuItem{   public string Label { get; set; }   public string Listview { get; set; }}

And create a new Menu class that has a list of MenuItems:

public class Menu{    public List<MenuItem> MenuItems { get; set; }}

See if that works.