c# - Trouble parsing JSON Data -


i have following json data webservice:

 {"zip_codes":[{"zip_code":"36451","distance":0,"city":"grove    hill","state":"al"},   {"zip_code":"36446","distance":8.031,"city":"fulton","state":"al"}]} 

i using newtonsoft.json library .net , trying parse above string cities. keep getting error.

public list<string> getzipcodeswithinradius(string zipcode, string radius)         {             list<string> l = new list<string>();              string apikey = @"----";             string apiformat = @"radius.json";             string baseurl = @"http://www.zipcodeapi.com/rest/";              string url = baseurl + apikey + @"/" + apiformat + @"/" + zipcode + @"/" + radius + @"/mile";               webclient client = new webclient();              stream stream = client.openread(@"http://www.zipcodeapi.com/rest/uoruearst9ktqo0ouifxxwh0up7ux6asu2mzcesgncan1bcvjhtqnbri0ynx4oqz/radius.json/36451/10/mile");              streamreader rdr = new streamreader(stream);              list<zipcode> ll = newtonsoft.json.jsonconvert.deserializeobject<list<zipcode>>(rdr.read().tostring());               foreach (zipcode z in ll)             {                 l.add(z.city);             }             return l;         } 

causes following error:

error converting value 123 type 'system.collections.generic.list`1[petrologic.tools.zipcode]'. path '', line 1, position 3. 

here object:

 public class zipcode     {         public zipcode() { }         [jsonproperty(propertyname="zip_code")]         public string zip_code { get; set; }          [jsonproperty(propertyname = "distance")]         public double distance { get; set; }          [jsonproperty(propertyname = "city")]         public string city { get; set; }          [jsonproperty(propertyname = "state")]         public string state { get; set; }     } 

please help

first, go http://json2csharp.com/ , post json there, , following c# classes:

public class zipcode {     public string zip_code { get; set; }     public double distance { get; set; }     public string city { get; set; }     public string state { get; set; } }  public class rootobject {     public list<zipcode> zip_codes { get; set; } } 

next, rather streaming json string deserializing string, can deserialize stream directly.

finally, sure wrap disposables in using statement.

thus:

    public list<string> getzipcodeswithinradius(string zipcode, string radius)     {         list<string> zipcodes = new list<string>();          string apikey = @"---removed answer---";         string apiformat = @"radius.json";         string baseurl = @"http://www.zipcodeapi.com/rest/";          string url = baseurl + apikey + @"/" + apiformat + @"/" + zipcode + @"/" + radius + @"/mile";          using (var client = new webclient())         using (var stream = client.openread(url))         using (var streamreader = new streamreader(stream))         using (var jsonreader = new jsontextreader(streamreader))         {             var root = new jsonserializer().deserialize<rootobject>(jsonreader);             if (root != null)             {                 foreach (zipcode z in root.zip_codes)                 {                     zipcodes.add(z.city);                 }             }         }         return zipcodes;     } 

by way, you're not passing input arguments service -- url used hardcoded. test code? seems so, because if client.openread(url) works expected.

also way, shouldn't method called getcitieswithinradius()? it's returning cities not zip codes after all. also, cities more 1 zip code, returns duplicated city names.


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -