0

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!

2
  • 1
    I used your link to generating C# classes from your JSON, its return below class as root that differs with your root class! public class Root { public List<Datum> data { get; set; } } Commented May 30, 2021 at 20:16
  • 2
    No need to use a third party website to generate your classes if you're using VS. Just, Edit->Paste Special->JSON as Classes. Commented May 30, 2021 at 20:37

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

6 Comments

I see that I have had a mess, but now, trying this code, it says Error reading JArray from JsonReader. Current JsonReader item is not an array: StartObject. Path '', line 1, position 1, its becouse my json starts with {data": before the [ of the array?
Added screenshot for the same
You have some problem related to the Json you are passing
Added the json that I used. Use it to test your solution.
Using yours, in my case the error persists, i see now the error is in my json format, should i post new question for that issue?
|

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.