locked
Unable to json deserialize correctly

    Question

  • Hello All,

    I am unable to deserialize json response correctly. Please help me. My json are below.

    { "top-cities":[{"parent_id":"4","city_id":"5","city_name":"Delhi & NCR"},{"parent_id":"4","city_id":"6","city_name":"Mumbai"},{"parent_id":"4","city_id":"20","city_name":"Chennai"},{"parent_id":"4","city_id":"15","city_name":"Patna"},{"parent_id":"4","city_id":"9","city_name":"Kolkata"},{"parent_id":"4","city_id":"18","city_name":"Goa"},{"parent_id":"4","city_id":"19","city_name":"Shimla"},{"parent_id":"4","city_id":"25","city_name":"Ranchi"},{"parent_id":"4","city_id":"98","city_name":"Jammu"},{"parent_id":"4","city_id":"99","city_name":"Bhopal"},{"parent_id":"4","city_id":"10","city_name":"Chandigarh"},{"parent_id":"4","city_id":"16","city_name":"Hyderabad"},{"parent_id":"4","city_id":"12","city_name":"Ahmedabad"},{"parent_id":"4","city_id":"7","city_name":"Lucknow"},{"parent_id":"4","city_id":"8","city_name":"Bangalore"},{"parent_id":"4","city_id":"100","city_name":"Raipur"},{"parent_id":"4","city_id":"11","city_name":"Jaipur"},{"parent_id":"4","city_id":"14","city_name":"North East"},{"parent_id":"4","city_id":"13","city_name":"Thiruvananthapuram"},{"parent_id":"4","city_id":"101","city_name":"Dehradun"},{"parent_id":"4","city_id":"22","city_name":"Bhubaneswar"}]} 

    I created a class root JsonResponse and declare a class name "top-cities" into a "JsonResponse" class and in "top-cities" class i also declare all the properties of this class. 

    public class JsonResponse
        {
            public List<top_cities> top_cities = new List<top_cities>();
        }
    public class top_cities
        {
            public string parent_id { get; set; }
            public string city_id { get; set; }
            public string city_name { get; set; }
        }


    ServiceResponse.JsonResponse mres = JsonConvert.DeserializeObject<ServiceResponse.JsonResponse>(results);

    in mres Count getting zero. which is wrong.

    Also when i try to create "top-cities" class in VS2013 it change its name to "top_cities". I dont know why. May it is problem or something wrong in my code. please suggest me.

    Thanks in Advance.

    Wednesday, July 23, 2014 4:47 AM

Answers

All replies