0

Greeting,

I am finding difficulty in parsing a JSON format file in c# having an array of highly nested objects which looks as follows

[
    {
        "id": "0001",
        "type": "donut",
        "name": "Cake",
        "ppu": 0.55,
        "batters":
            {
                "batter":
                    [
                        { "id": "1001", "type": "Regular" },
                        { "id": "1002", "type": "Chocolate" },
                        { "id": "1003", "type": "Blueberry" },
                        { "id": "1004", "type": "Devil's Food" }
                    ]
            },
        "topping":
            [
                { "id": "5001", "type": "None" },
                { "id": "5002", "type": "Glazed" },
                { "id": "5005", "type": "Sugar" },
                { "id": "5007", "type": "Powdered Sugar" },
                { "id": "5006", "type": "Chocolate with Sprinkles" },
                { "id": "5003", "type": "Chocolate" },
                { "id": "5004", "type": "Maple" }
            ]
    },
    {
        "id": "0002",
        "type": "donut",
        "name": "Raised",
        "ppu": 0.55,
        "batters":
            {
                "batter":
                    [
                        { "id": "1001", "type": "Regular" }
                    ]
            },
        "topping":
            [
                { "id": "5001", "type": "None" },
                { "id": "5002", "type": "Glazed" },
                { "id": "5005", "type": "Sugar" },
                { "id": "5003", "type": "Chocolate" },
                { "id": "5004", "type": "Maple" }
            ]
    },
    {
        "id": "0003",
        "type": "donut",
        "name": "Old Fashioned",
        "ppu": 0.55,
        "batters":
            {
                "batter":
                    [
                        { "id": "1001", "type": "Regular" },
                        { "id": "1002", "type": "Chocolate" }
                    ]
            },
        "topping":
            [
                { "id": "5001", "type": "None" },
                { "id": "5002", "type": "Glazed" },
                { "id": "5003", "type": "Chocolate" },
                { "id": "5004", "type": "Maple" }
            ]
    }
]

I am looking for a solution like "id", "type","name", "ppu" as private members of a class and "batters" and "topping" as dictionary members. Kindly suggest me the better way in getting it parsed.

Thank you.

5
  • 1
    json2csharp.com Commented Apr 6, 2017 at 14:33
  • Possible duplicate of How can I parse JSON with C#? Commented Apr 6, 2017 at 14:34
  • 4
    You say you're "finding difficult" which suggests you've tried something that hasn't worked: so show us that, and we'll be able to help you fix it. Commented Apr 6, 2017 at 14:34
  • Does this JSON have corresponding class in your code? Commented Apr 6, 2017 at 14:35
  • Consider using nuget Newtonsoft.Json in your project. Then you can just deal with it already parsed. Commented Apr 6, 2017 at 14:49

1 Answer 1

1

Following class structure will help you to parse JSON to C# object.

public class Batter
{
    public string id { get; set; }
    public string type { get; set; }
}

public class Batters
{
    public List<Batter> batter { get; set; }
}

public class Topping
{
    public string id { get; set; }
    public string type { get; set; }
}

public class RootObject
{
    public string id { get; set; }
    public string type { get; set; }
    public string name { get; set; }
    public double ppu { get; set; }
    public Batters batters { get; set; }
    public List<Topping> topping { get; set; }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Yes. That Helped. Thank you your quick response.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.