Toast Bread
Toast Bread

Reputation: 29

Problems deserializing Newtonsoft JSON in c# returning null

I'm trying to deserialize a json in c# with Newtonsoft, but when i try to print the elements, it returns null.

The json is the following:

{
"data": [
    {
        "ufs": [
            {
                "delivery": [
                    {
                        "grade": "100",
                        "name": "P01",
                        "id": 10,
                        "status": "submitted"
                    },
                    {
                        "name": "P02",
                        "id": 11,
                        "status": "new"
                    }
                ],
                "name": "UF1",
                "id": "18"
            },
            {
                "delivery": [
                    {
                        "name": "P03",
                        "id": 12,
                        "status": "new"
                    },
                    {
                        "name": "P04",
                        "id": 13,
                        "status": "new"
                    }
                ],
                "name": "UF2",
                "id": "19"
            }
        ],
        "name": "M1",
        "id": "5"
    },
    {
        "ufs": [
            {
                "delivery": [
                    {
                        "name": "P01",
                        "id": 6,
                        "status": "submitted"
                    },
                    {
                        "name": "P02",
                        "id": 7,
                        "status": "new"
                    }
                ],
                "name": "UF1",
                "id": "23"
            },
            {
                "delivery": [
                    {
                        "name": "P03",
                        "id": 8,
                        "status": "new"
                    },
                    {
                        "name": "P04",
                        "id": 9,
                        "status": "new"
                    }
                ],
                "name": "UF2",
                "id": "24"
            }
        ],
        "name": "M2",
        "id": "6"
    }
]
}

So, having this JSON, i went to this site to generate the classes i would need, that are the following ones:

    public class Delivery
{
    public string grade { get; set; }
    public string name { get; set; }
    public int id { get; set; }
    public string status { get; set; }
}

public class Uf
{
    public List<Delivery> delivery { get; set; }
    public string name { get; set; }
    public string id { get; set; }
}

public class Root
{
    public List<Uf> ufs { get; set; }
    public string name { get; set; }
    public string id { get; set; }
}

So finally, when i deserialize the JSON with this line

List<Root> myDeserializedClass = JsonConvert.DeserializeObject<List<Root>>(json);

And try to print

foreach (Root item in myDeserializedClass)
        {
            Console.WriteLine(item.ufs.Count);
        }

It says "Root.ufs.get returned null."

Any clue of where im mistaking? Thanks for the help!

Grettings from Spain!

Upvotes: 0

Views: 166

Answers (1)

Amit Kotha
Amit Kotha

Reputation: 2019

You class structure would be like this

public class Delivery
        {
            public string grade { get; set; }
            public string name { get; set; }
            public int id { get; set; }
            public string status { get; set; }
        }

        public class Uf
        {
            public List<Delivery> delivery { get; set; }
            public string name { get; set; }
            public string id { get; set; }
        }

        public class Data
        {
            public List<Uf> ufs { get; set; }
            public string name { get; set; }
            public string id { get; set; }
        }

        public class Root
        {
            public List<Data> data { get; set; }
        }

When you needed to deserialize the Json you will do like this

Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(json);

Here is the json that I used

string json = @"{'data': [
    {
                'ufs': [
                    {
                    'delivery': [
                        {
                        'grade': '100',
                        'name': 'P01',
                        'id': 10,
                        'status': 'submitted'
                        },
                    {
                        'name': 'P02',
                        'id': 11,
                        'status': 'new'
                    }
                ],
                'name': 'UF1',
                'id': '18'
                    },
            {
                    'delivery': [
                        {
                        'name': 'P03',
                        'id': 12,
                        'status': 'new'
                        },
                    {
                        'name': 'P04',
                        'id': 13,
                        'status': 'new'
                    }
                ],
                'name': 'UF2',
                'id': '19'
            }
        ],
        'name': 'M1',
        'id': '5'
    },
    {
                'ufs': [
                    {
                    'delivery': [
                        {
                        'name': 'P01',
                        'id': 6,
                        'status': 'submitted'
                        },
                    {
                        'name': 'P02',
                        'id': 7,
                        'status': 'new'
                    }
                ],
                'name': 'UF1',
                'id': '23'
                    },
            {
                    'delivery': [
                        {
                        'name': 'P03',
                        'id': 8,
                        'status': 'new'
                        },
                    {
                        'name': 'P04',
                        'id': 9,
                        'status': 'new'
                    }
                ],
                'name': 'UF2',
                'id': '24'
            }
        ],
        'name': 'M2',
        'id': '6'
    }
]
}";

enter image description here

Upvotes: 1

Related Questions