deserialize json string for multiple results

rbur0425 picture rbur0425 · Mar 2, 2011 · Viewed 7.9k times · Source

I am using Json.Net however when I go to deserialize the following json I get that Json cannot deserialize to type List.

Json:

{"postalCodes":[{"adminName2":"New York","adminCode2":"061","adminCode1":"NY","postalCode":"10001","countryCode":"US","lng":-73.996705,"placeName":"New York City","lat":40.74838,"adminName1":"New York"},{"adminName2":"New York","adminCode2":"061","adminCode1":"NY","postalCode":"10019","countryCode":"US","lng":-73.985834,"placeName":"New York City","lat":40.765069,"adminName1":"New York"},{"adminName2":"New York","adminCode2":"061","adminCode1":"NY","postalCode":"10021","countryCode":"US","lng":-73.958805,"placeName":"New York City","lat":40.768476,"adminName1":"New York"},{"adminName2":"New York","adminCode2":"061","adminCode1":"NY","postalCode":"10022","countryCode":"US","lng":-73.965703,"placeName":"New York City","lat":40.757091,"adminName1":"New York"},{"adminName2":"New York","adminCode2":"061","adminCode1":"NY","postalCode":"10036","countryCode":"US","lng":-73.991826,"placeName":"New York City","lat":40.759724,"adminName1":"New York"},{"adminName2":"New York","adminCode2":"061","adminCode1":"NY","postalCode":"10065","countryCode":"US","lng":-73.96379,"placeName":"New York City","lat":40.76507,"adminName1":"New York"},{"adminName2":"New York","adminCode2":"061","adminCode1":"NY","postalCode":"10003","countryCode":"US","lng":-73.989223,"placeName":"New York City","lat":40.731253,"adminName1":"New York"},{"adminName2":"New York","adminCode2":"061","adminCode1":"NY","postalCode":"10010","countryCode":"US","lng":-73.981328,"placeName":"New York City","lat":40.737476,"adminName1":"New York"},{"adminName2":"New York","adminCode2":"061","adminCode1":"NY","postalCode":"10011","countryCode":"US","lng":-73.99963,"placeName":"New York City","lat":40.740225,"adminName1":"New York"},{"adminName2":"New York","adminCode2":"061","adminCode1":"NY","postalCode":"10012","countryCode":"US","lng":-73.998284,"placeName":"New York City","lat":40.72553,"adminName1":"New York"}]}

code:

string jsonResponse = string.empty;

using (StreamReader sr = new StreamReader(client.OpenRead(address.AbsoluteUri)))
            {
                jsonResponse = sr.ReadLine();
            }


            var observation = JsonConvert.DeserializeObject<List<postalCodes>>(jsonResponse); 
            return observation; 

postalCodes class:

public class postalCodes
{

    public string adminName2 { get; set; }

    public int adminCode2 { get; set; }

    public string adminCode1 {get; set; }

    public int postalCode { get; set; }

    public string countryCode { get; set; }

    public double longitude { get; set; }

    public string placeName { get; set; }

    public double latitude { get; set; }

    public string adminName1 { get; set; }
}

Answer

Philipp picture Philipp · Mar 2, 2011

Please try this:

public class postalCodes
        {

            public string adminName2 { get; set; }

            public int adminCode2 { get; set; }

            public string adminCode1 { get; set; }

            public int postalCode { get; set; }

            public string countryCode { get; set; }

            public double lng { get; set; }

            public string placeName { get; set; }

            public double lat { get; set; }

            public string adminName1 { get; set; }
        }
        public class DataContainer
        {
            public List<postalCodes> postalCodes { get; set; }
        }
        [Test]
        public void TestJson()
        {
            string input = "{\"postalCodes\":[{\"adminName2\":\"New York\",\"adminCode2\":\"061\",\"adminCode1\":\"NY\",\"postalCode\":\"10001\",\"countryCode\":\"US\",\"lng\":-73.996705,\"placeName\":\"New York City\",\"lat\":40.74838,\"adminName1\":\"New York\"},{\"adminName2\":\"New York\",\"adminCode2\":\"061\",\"adminCode1\":\"NY\",\"postalCode\":\"10019\",\"countryCode\":\"US\",\"lng\":-73.985834,\"placeName\":\"New York City\",\"lat\":40.765069,\"adminName1\":\"New York\"},{\"adminName2\":\"New York\",\"adminCode2\":\"061\",\"adminCode1\":\"NY\",\"postalCode\":\"10021\",\"countryCode\":\"US\",\"lng\":-73.958805,\"placeName\":\"New York City\",\"lat\":40.768476,\"adminName1\":\"New York\"},{\"adminName2\":\"New York\",\"adminCode2\":\"061\",\"adminCode1\":\"NY\",\"postalCode\":\"10022\",\"countryCode\":\"US\",\"lng\":-73.965703,\"placeName\":\"New York City\",\"lat\":40.757091,\"adminName1\":\"New York\"},{\"adminName2\":\"New York\",\"adminCode2\":\"061\",\"adminCode1\":\"NY\",\"postalCode\":\"10036\",\"countryCode\":\"US\",\"lng\":-73.991826,\"placeName\":\"New York City\",\"lat\":40.759724,\"adminName1\":\"New York\"},{\"adminName2\":\"New York\",\"adminCode2\":\"061\",\"adminCode1\":\"NY\",\"postalCode\":\"10065\",\"countryCode\":\"US\",\"lng\":-73.96379,\"placeName\":\"New York City\",\"lat\":40.76507,\"adminName1\":\"New York\"},{\"adminName2\":\"New York\",\"adminCode2\":\"061\",\"adminCode1\":\"NY\",\"postalCode\":\"10003\",\"countryCode\":\"US\",\"lng\":-73.989223,\"placeName\":\"New York City\",\"lat\":40.731253,\"adminName1\":\"New York\"},{\"adminName2\":\"New York\",\"adminCode2\":\"061\",\"adminCode1\":\"NY\",\"postalCode\":\"10010\",\"countryCode\":\"US\",\"lng\":-73.981328,\"placeName\":\"New York City\",\"lat\":40.737476,\"adminName1\":\"New York\"},{\"adminName2\":\"New York\",\"adminCode2\":\"061\",\"adminCode1\":\"NY\",\"postalCode\":\"10011\",\"countryCode\":\"US\",\"lng\":-73.99963,\"placeName\":\"New York City\",\"lat\":40.740225,\"adminName1\":\"New York\"},{\"adminName2\":\"New York\",\"adminCode2\":\"061\",\"adminCode1\":\"NY\",\"postalCode\":\"10012\",\"countryCode\":\"US\",\"lng\":-73.998284,\"placeName\":\"New York City\",\"lat\":40.72553,\"adminName1\":\"New York\"}]}";
            var observation = JsonConvert.DeserializeObject<DataContainer>(input); 
        }