1

i need to convert the below JSON format to an object, my Class properties are

        public string address_line_1 { get; set; }
        public string locality { get; set; }
        public string region { get; set; }
        public string permises { get; set; }
        public string postal_code { get; set; }

and the JSON that i get is

 {  
   "items_per_page":2,
   "items":[  
      {  
         "title":"Info",
         "description":"02506398 -  ",
         "links":{  
            "self":"/company/02506398"
         },
         "company_number":"11111",
         "company_status":"active",
         "address":{  
            "region":"Somewhere  ,",
            "postal_code":"TX1 7JQ",
            "locality":"Somewhere  , Somewhere  Mill",
            "premises":"Somewhere  House",
            "address_line_1":"Somewhere  Road"
         },
         "matches":{  
            "snippet":[  

            ],
            "title":[  
               1,
               7,
               9,
               12
            ]
         },
         "description_identifier":[  
            "incorporated-on"
         ],
         "kind":"searchresults#company",
         "date_of_creation":"1990-05-29",
         "company_type":"ltd",
         "snippet":"",
         "address_snippet":"Somewhere  House, Somewhere  Road, Somewhere  , Somewhere  Mill, Somewhere  ,, TX1 7JQ"
      },
      {  


}

the only information that i need to get are Address section and i have tried with the code below

dynamic x = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(t, new Newtonsoft.Json.JsonSerializerSettings() { NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore });

Or

Newtonsoft.Json.JsonConvert.DeserializeObject<List<Address>>(t).ForEach(cc =>
                {
                    newAddress.address_line_1 = cc.address_line_1;
                    newAddress.locality = cc.locality;
                    newAddress.permises = cc.permises;
                    newAddress.region = cc.region;
                    newAddress.postal_code = cc.postal_code;

                });

But i can not find the right information so any type of help would be great `

Reza Del
  • 763
  • 10
  • 30
  • Possible duplicate of [Newtonsoft.JSON cannot convert model with TypeConverter attribute](https://stackoverflow.com/questions/31325866/newtonsoft-json-cannot-convert-model-with-typeconverter-attribute) Try that approach using a Converter – Zinov Jun 14 '17 at 16:23

1 Answers1

2

The easiest way is to create classes for all properties in your JSON. There's a very helpful tool for this: https://jsonutils.com/ Remember to select JsonProperty in picker.

Then, you can just use:var object = JsonConvert.DeserializeObject<MainClass>(json);

Finally, you can access your data using LINQ query on your object to get what you need.

Jakub Nowacki
  • 121
  • 2
  • 11
  • I have change it to {result = Newtonsoft.Json.JsonConvert.DeserializeObject(request, new Newtonsoft.Json.JsonSerializerSettings() { NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore });} but your answer did help , Thanks – Reza Del Jun 15 '17 at 13:14