0

So after doing a web request, I get this JSON string back:

{"status":"okay","result":[{"id":8810,"country":"IE","region":"07","city":"Dublin","latitude":53.3331,"longitude":-6.2489,"comment":"407367 donkeys"},{"id":9688,"country":"IE","region":"04","city":"Cork","latitude":51.8986,"longitude":-8.4958,"comment":"454765 donkeys"},{"id":9963,"country":"IE","region":"06","city":"Donegal","latitude":54.65,"longitude":-8.1167,"comment":"315518 donkeys"}]}

I'm not sure how to parse it. I have a City class that has id, country, region etc, and I would like to be able to save each one separately in a list so I can add them to a List View for an app.

I have tried this:

JObject jobj = JObject.Parse(jsonString);
JToken jstatus = jobj["status"];
JToken jresult = jobj["result"];
status = (String)jstatus;
JArray arrayOfCities = JArray.Parse(jsonString);

        if (status.Equals("okay"))
        {
            foreach (JObject o in arrayOfCities.Children<JObject>())
            {
                foreach (JProperty p in o.Properties())
                {
                    id = p.Name + p.Value.ToString();// (String)jresult["id"];

                    country = (String)jresult["country"];
                    region = (String)jresult["region"];
                    city = (String)jresult["city"];
                    latitude = (String)jresult["latitude"];
                    longitude = (String)jresult["longitude"];
                    comment = (String)jresult["comment"];
                }
            }
        }

However I keep getting parse errors in it. I tried a few things, but none end up working. How could I parse each part of the array separately and save it to a City List. Thanks

2
  • 2
    JArray arrayOfCities = JArray.Parse(jsonString); should probably read "JArray arrayOfCities = JArray.Parse(jresult);" Commented Nov 12, 2013 at 23:13
  • @It wont work because jsonString is a string filled with the web request i have at the top of my post. jresult is a JToken Commented Nov 12, 2013 at 23:17

1 Answer 1

9

You should use DeserializeObject. Here's an example:

public class CitiesResponse
{
    public string Status { get; set; }
    public List<City> Result { get; set; }
}
var response = JsonConvert.DeserializeObject<CitiesResponse>(jsonString);
// response.Result is your list of cities
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the help. However, what do do when I do List<Cities> t = response.Result? I can't view whats inside the object when I set breakpoints so I don't know if it's working?
You can put a breakpoint on any line after var response = ... and inspect the resulting objects in your debugger, or put lines outputting data about it.
Hm, your City class should be roughly like this in order to work: pastebin.com/djDKYaJs (generated with json2csharp.com) Json.net is pretty flexible, however, especially when you configure it with attributes. If you can't figure it out, you can ask a new question about this. If you do that, include your City and CitiesResponse classes and example JSON data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.