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.

RCM
  • 91
  • 3
  • 10
  • 2
    Unsure if it's just a typo, but you're missing a double-quote before `type` in your JSON. – Adam V Jun 23 '16 at 17:21
  • Take a look at JsonSchema.Parse(). The similar question was here: http://stackoverflow.com/questions/23906220/deserialize-json-in-a-tryparse-way – Alex Yagur Jun 23 '16 at 17:24

2 Answers2

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.

Fran
  • 6,440
  • 1
  • 23
  • 35
  • tried what? fixing the json or changing your model? – Fran Jun 23 '16 at 17:56
  • solution which you mentioned – RCM Jun 23 '16 at 18:03
  • so you changed the json to this? var jsonObject = { "name": "sections", "record": [ { "id": 1, "name": "sections", "tables": [ { "sections": [ { "id": "1", "type": "2" } ] } ] } ] } – Fran Jun 23 '16 at 18:45
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"
Tinwor
  • 7,765
  • 6
  • 35
  • 56