简体   繁体   中英

Nested JSON-object in c#

I want to create a multidimensional json object based on ac# class. I normally do it like this:

public class foo
{
    public string name { get; set; }
    public int age { get; set; }
}

And serialize a new instance of the class with a JavaScriptSerializer. But lets say that i want to add another json array containing the persons friends inside the main json array. Example array: Accessing data in a multidimensional JSON array with jQuery

Hope you get the idea. Thanks

If I get your question corect, something like this should work...

public class Person 
{     
  public string name { get; set; }     
  public int age { get; set; } 
  public List<Person> Friends { get; set; }
} 

How are you creating the JSON version of your class, foo?

JavaScriptSerializer and Controller.JSON (in MVC) support serializing IEnumerable derived types, so you could have the class:

public class foo
{
    public string name { get; set; }
    public int age { get; set; }
    public List<Bar> friends {get; set;}
}

and your JSON serialized class would include the list of Bar objects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM