0

You can enter either of these URLs into a browser and verify that they return valid xml:

http://maps.googleapis.com/maps/api/geocode/xml?`address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false"

...or json:

http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false%22

...but when attempting to get the xml programmatically using this code (after installing Json.NET via NuGet into my project and dropping a dataGridView on my Windows form):

dataGridView1.DataSource = GetLocationData("http://maps.googleapis.com/maps/api/geocode/xml?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false");

private JArray GetLocationData(string uri)
{
    var webRequest = (HttpWebRequest)WebRequest.Create(uri);
    var webResponse = (HttpWebResponse)webRequest.GetResponse();
    var reader = new StreamReader(webResponse.GetResponseStream());
    string s = reader.ReadToEnd();
    return JsonConvert.DeserializeObject<JArray>(s);
}

...I get: Newtonsoft.Json.JsonReaderException was unhandled _message=Unexpected character encountered while parsing value: <. Path '', line 0, position 0.

I thought, okay, this actually is json code, not xml, so I replaced "xml" with "json" in the URL, expecting more joy in Mudville.

However, when attempting to get the same data as json, I also get an exception, namely, "System.InvalidCastException was unhandled _message=Unable to cast object of type 'Newtonsoft.Json.Linq.JObject' to type 'Newtonsoft.Json.Linq.JArray'."

Why, and how can I fix it? Is it possible to also grab the data as xml?

B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862

1 Answers1

1

Using the json URL is correct. Your problem is that the JSON response is not giving you an array which is your error.

Notice that the response you get back is an object denoted by the {

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "1600",
               "short_name" : "1600",
               "types" : [ "street_number" ]
            },
    ....

If it was an array, you would get back [ instead. You should use JObject or a custom model instead of JArray. The results element is what contains the actual array that you will need to loop through.

Pseudo for the result you are actually getting in the response:

object
{
    Result[] results;
    string status;
}

You can also grab the XML version if you want, but then you need to actually have an object model defined to match and use the XmlSerializer to deserialize it or load it into an XmlDocument (lots of ways to work with XML). However, you cannot pass XML into the JSON serializer as it is expecting a JSON string.

EDIT: There are methods on JsonConvert to convert from XML. See this answer here: How to convert JSON to XML or XML to JSON?

Community
  • 1
  • 1
TyCobb
  • 8,909
  • 1
  • 33
  • 53
  • Changing JArray to JObject in both places allows the code to run exception-free; the DataGridView is not populated, though. I could swear I had this working several months ago (with different data). – B. Clay Shannon-B. Crow Raven Oct 01 '14 at 23:13
  • 1
    @B.ClayShannon I have not use the `DataGridView` before. I found this article http://www.codeproject.com/Tips/712109/How-to-Get-REST-Data-and-Display-it-in-a-DataGridV , but it appears you are the author =P. My guess is that either the response on that page changed or you need to extract the `results` JArray out of the JObject you deserialized and return that instead. – TyCobb Oct 01 '14 at 23:17
  • Yeah, that's when it was working for me - revisiting it, and it fails! Bizarre... – B. Clay Shannon-B. Crow Raven Oct 01 '14 at 23:20