简体   繁体   中英

Return Nested json objects using json.net in a list

how can return a nested json objects like this using json.net:

How i set my clases to return this

        [  {
            "name": "Jasmine Coots",
            "sex": "female",
            "age": 49,
            "Sons": [
                {
                  "name": "Morgan Rhett",
                  "age": 25,
                  "sex": "male"
                },
                {
                  "name": " Jasmine Coots ",
                  "age": 22,
                  "sex": "female"
                },
                {
                  "name": "Joaquin Strother",
                  "age": 17,
                  "sex": "male"
                }
              ]
        },
 {
            "name": "Jasmine Coots",
            "sex": "female",
            "age": 49,
            "Sons": [
                {
                  "name": "Morgan Rhett",
                  "age": 25,
                  "sex": "male"
                },
                {
                  "name": " Jasmine Coots ",
                  "age": 22,
                  "sex": "female"
                },
                {
                  "name": "Joaquin Strother",
                  "age": 17,
                  "sex": "male"
                }
              ]
        }
            ]

I need to return an array of objects using a list in c# an a datatable or datareader any example of how do this would be great

Life tip: Check out this site, it's amazing. The only thing I don't particularly like, is it doesn't automatically use conventional property names ( PascalCase ) , but it's really handy. You can simply make the adjustments yourself, as long as it is still spelled correctly.

Now, using it you can generate the classes you need to deserialize this puppy.

public class Son
{
    public string name { get; set; }
    public int age { get; set; }
    public string sex { get; set; }
}

public class RootObject
{
    public string name { get; set; }
    public string sex { get; set; }
    public int age { get; set; }
    public List<Son> Sons { get; set; }
}

Usage:

var jsonString = "this is your JSON data";
var root = JsonConvert.DeserializeObject<RootObject>(jsonString);

// do stuff

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