0

I want to deserialize the below Json object,

var jsonObject = {
    "name": "sections",
    "record": [
        { 
            "id": 1,
            "name": "sections",
            "tables": 
            {
                "sections": 
                {
                    "id": "1",
                    "type": "2"
                }
            }
        }
    ]
}

In C#

var result = JsonConvert.DeserializeObject<Response>(jsonObject);

Added below classes for deserialize

public class Response
        {
            [JsonProperty("name")]
            public string Name;

            [JsonProperty("record")]
            public List<Records> Record;
        }

        public class Records
        {
            [JsonProperty("id")]
            public int Id;

            [JsonProperty("name")]
            public string Name;

            [JsonProperty("tables")]
            public List<Table> Tables;

        }

        public class Table
        {
            [JsonProperty("sections")]
            public List<Sections> Sections;
        }

        public class Sections
        {
            [JsonProperty("id")]
            public string id;

            [JsonProperty("type")]
            public string Type;

        }

I want to get the "Type" from the json, but it is not deserialized properly. Can anyone suggest how to get the Type from the Json Object.

2
  • 2
    Unsure if it's just a typo, but you're missing a double-quote before type in your JSON. Commented Jun 23, 2016 at 17:21
  • Take a look at JsonSchema.Parse(). The similar question was here: stackoverflow.com/questions/23906220/… Commented Jun 23, 2016 at 17:24

2 Answers 2

1

From the question, the class doesn't match.

public class Response
        {
            public string Name;
            public List<Records> Record;
        }

        public class Records
        {
            public int Id;
            public string Name;
            public List<Table> Tables;
        }

        public class Table
        {
            public List<Sections> Sections;
        }

        public class Sections
        {
            public string id;
            public string Type;

        }

Sections doen't have [ ], neither does tables, so they are not lists.

I'd also change you deserialization code to this

var result = JsonConvert.DeserializeObject<Response>(jsonObject, new JsonSerializerSettings
                {
                    ContractResolver = new CamelCasePropertyNamesContractResolver()
                });

So that you aren't annotating every class property just for camel cased JSON.

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

3 Comments

tried what? fixing the json or changing your model?
solution which you mentioned
so you changed the json to this? var jsonObject = { "name": "sections", "record": [ { "id": 1, "name": "sections", "tables": [ { "sections": [ { "id": "1", "type": "2" } ] } ] } ] }
0

You cannot serialize the object since the property type is not correctly formatted.
Instead of

type ": "
                    2 "

you must set type as follow

"type":"2"

Comments

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.