У меня проблемы с получением значений из результатов JSON, полученных из API Google Geo Code
Функция
public static async Task GetLatLogFromAddress()
{
using (var client = new HttpClient())
{
Uri Inputuri = new Uri("https://maps.googleapis.com/maps/api/geocode/json?address=123+N+TRY+Street+Paul,+MN&key=APIKey");
client.BaseAddress = Inputuri;
// Add an Accept header for JSON format.
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
// List data response.
HttpResponseMessage response = await client.GetAsync(client.BaseAddress); ; // Blocking call! Program will wait here until a response is received or a timeout occurs.
if (response.IsSuccessStatusCode)
{
// Parse the response body
string result = await response.Content.ReadAsStringAsync();
dynamic jsonData = JsonConvert.DeserializeObject<dynamic>(result);
foreach(var geo in jsonData)
{
var lat = (decimal)geo[0].geometry.location.lat;
var longi = (decimal)geo[0].geometry.location.lng;
}
}
else
{
Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
}
client.Dispose();
}
}
Результаты в формате JSON:
{
"results" : [
{
"address_components" : [
{
"long_name" : "Winnetka",
"short_name" : "Winnetka",
"types" : [ "neighborhood", "political" ]
},
{
"long_name" : "Los Angeles",
"short_name" : "LA",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Los Angeles County",
"short_name" : "Los Angeles County",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "California",
"short_name" : "CA",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Winnetka, Los Angeles, CA, USA",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 34.2355209,
"lng" : -118.5534191
},
"southwest" : {
"lat" : 34.1854649,
"lng" : -118.588536
}
},
"location" : {
"lat" : 34.2048586,
"lng" : -118.5739621
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 34.2355209,
"lng" : -118.5534191
},
"southwest" : {
"lat" : 34.1854649,
"lng" : -118.588536
}
}
},
"place_id" : "ChIJ0fd4S_KbwoAR2hRDrsr3HmQ",
"types" : [ "neighborhood", "political" ]
}
],
"status" : "OK"
}
Это результаты, которые мы получаем из Google GeoCode API
Я пытаюсь получить Широту и Долготу, но получаю ошибку, что Geo не содержит определения геометрии. Как получить доступ к результатам JSON, полученным из API Google Geo Code?