Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

Querying JSON with dynamic

$
0
0

I'm using GoogleMapsApi to get the longitude and latitude information for a given address. I'm now attempting to extract those two fields from the resulting JSON string using a dynamic object. When I run the code its not finding the specific fields that I'm trying to get:

            using (WebClient wc = new WebClient())
            {
                var json = wc.DownloadString("https://maps.googleapis.com/maps/api/geocode/json?address=M16%208NQ&key=AIzaSyAfPTSq3iq4uNL1vjvZ4lcKcYVAT4pBTuQ");
                dynamic data = JObject.Parse(json);
                location.Latitude = data.results.geometry.location.lat;
                location.Longitude = data.results.geometry.location.lng;

            }

In debug I can see that both data and data.results provide information but when looking at geometry or anything further no values are present. Full text for the JSON string is:

{"results" : [
      {"address_components" : [
            {"long_name" : "M16 8NQ","short_name" : "M16 8NQ","types" : [ "postal_code" ]
            },
            {"long_name" : "Arnold Road","short_name" : "Arnold Rd","types" : [ "route" ]
            },
            {"long_name" : "Manchester","short_name" : "Manchester","types" : [ "locality", "political" ]
            },
            {"long_name" : "Manchester","short_name" : "Manchester","types" : [ "postal_town" ]
            },
            {"long_name" : "Greater Manchester","short_name" : "Greater Manchester","types" : [ "administrative_area_level_2", "political" ]
            },
            {"long_name" : "England","short_name" : "England","types" : [ "administrative_area_level_1", "political" ]
            },
            {"long_name" : "United Kingdom","short_name" : "GB","types" : [ "country", "political" ]
            }
         ],"formatted_address" : "Arnold Rd, Manchester M16 8NQ, UK","geometry" : {"bounds" : {"northeast" : {"lat" : 53.4447047,"lng" : -2.2519609
               },"southwest" : {"lat" : 53.44365639999999,"lng" : -2.2534188
               }
            },"location" : {"lat" : 53.444166,"lng" : -2.2527556
            },"location_type" : "APPROXIMATE","viewport" : {"northeast" : {"lat" : 53.44552953029149,"lng" : -2.251340869708498
               },"southwest" : {"lat" : 53.4428315697085,"lng" : -2.254038830291502
               }
            }
         },"place_id" : "ChIJN643bg-ye0gR11wlogjFTxY","types" : [ "postal_code" ]
      }
   ],"status" : "OK"


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>