如何从Google json阅读城市 [英] How to read city from Google json

查看:45
本文介绍了如何从Google json阅读城市的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下代码使用Google 反向地理编码:

I am trying to use Google Reverse Geocoding using below code:

var client = new HttpClient();
client.BaseAddress = new Uri("https://maps.googleapis.com/maps/api/geocode/json?latlng=25.2547667,55.290913&key=AIzaSyC3-0U0ngwYwfFhPeofMtm_C4VhIBMt77A&language=ar");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = await client.GetAsync("https://maps.googleapis.com/maps/api/geocode/json?latlng=25.2547667,55.290913&key=AIzaSyC3-xxxxxxxxx&language=ar");

if (response.IsSuccessStatusCode)
{
    var data = await response.Content.ReadAsStringAsync();

	dynamic storydata = JsonConvert.DeserializeObject(data);

    LabelCity.Text = storydata[0].results.address_components[0].long_name[2];
}



并低于结果。我想知道怎样才能将  results-> address_components [2] - > long_name读入Label控件?

and getting below result. I want to know how can I read the results->address_components[2]->long_name into a Label control?

结果如下:

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "11",
               "short_name" : "11",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "20 B Street",
               "short_name" : "20 B St",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Bur Dubai",
               "short_name" : "Bur Dubai",
               "types" : [ "political", "sublocality", "sublocality_level_1" ]
            },
            {
               "long_name" : "Dubai",
               "short_name" : "Dubai",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Dubai",
               "short_name" : "Dubai",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "الإمارات العربية المتحدة",
               "short_name" : "AE",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "11 20 B St - Dubai - الإمارات العربية المتحدة",
         "geometry" : {
            "location" : {
               "lat" : 25.2549008,
               "lng" : 55.2906922
            },
            "location_type" : "ROOFTOP",
            "viewport" : {
               "northeast" : {
                  "lat" : 25.2562497802915,
                  "lng" : 55.29204118029151
               },
               "southwest" : {
                  "lat" : 25.25355181970849,
                  "lng" : 55.28934321970851
               }
            }
         },
         "place_id" : "ChIJ94_LtRdDXz4RavTGBJ02bfc",
         "types" : [ "street_address" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "الرفاعة",
               "short_name" : "الرفاعة",
               "types" : [ "political", "sublocality", "sublocality_level_1" ]
            },
            {
               "long_name" : "دبي",
               "short_name" : "دبي",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "دبي",
               "short_name" : "دبي",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "الإمارات العربية المتحدة",
               "short_name" : "AE",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "الرفاعة - دبي - الإمارات العربية المتحدة",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 25.260437,
                  "lng" : 55.2967646
               },
               "southwest" : {
                  "lat" : 25.2470423,
                  "lng" : 55.28096799999999
               }
            },
            "location" : {
               "lat" : 25.255758,
               "lng" : 55.28808739999999
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 25.260437,
                  "lng" : 55.2967646
               },
               "southwest" : {
                  "lat" : 25.2470423,
                  "lng" : 55.28096799999999
               }
            }
         },
         "place_id" : "ChIJu7ZyDhdDXz4R6wfJPDoMXk0",
         "types" : [ "political", "sublocality", "sublocality_level_1" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "بر دبي",
               "short_name" : "بر دبي",
               "types" : [ "political", "sublocality", "sublocality_level_1" ]
            },
            {
               "long_name" : "دبي",
               "short_name" : "دبي",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "دبي",
               "short_name" : "دبي",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "الإمارات العربية المتحدة",
               "short_name" : "AE",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "بر دبي - دبي - الإمارات العربية المتحدة",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 25.2752418,
                  "lng" : 55.3470611
               },
               "southwest" : {
                  "lat" : 25.1948451,
                  "lng" : 55.2754784
               }
            },
            "location" : {
               "lat" : 25.2145565,
               "lng" : 55.3032906
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 25.2752418,
                  "lng" : 55.3470611
               },
               "southwest" : {
                  "lat" : 25.1948451,
                  "lng" : 55.2754784
               }
            }
         },
         "place_id" : "ChIJYXshnzlDXz4RiypxAjQP118",
         "types" : [ "political", "sublocality", "sublocality_level_1" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "دبي",
               "short_name" : "دبي",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "دبي",
               "short_name" : "دبي",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "الإمارات العربية المتحدة",
               "short_name" : "AE",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "دبي - الإمارات العربية المتحدة",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 25.3585607,
                  "lng" : 55.5650393
               },
               "southwest" : {
                  "lat" : 24.7921359,
                  "lng" : 54.89045429999999
               }
            },
            "location" : {
               "lat" : 25.2048493,
               "lng" : 55.2707828
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 25.3585607,
                  "lng" : 55.5650393
               },
               "southwest" : {
                  "lat" : 24.7921359,
                  "lng" : 54.89045429999999
               }
            }
         },
         "place_id" : "ChIJRcbZaklDXz4RYlEphFBu5r0",
         "types" : [ "locality", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "دبي",
               "short_name" : "دبي",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "الإمارات العربية المتحدة",
               "short_name" : "AE",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "دبي - الإمارات العربية المتحدة",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 25.3538421,
                  "lng" : 56.2046836
               },
               "southwest" : {
                  "lat" : 24.605052,
                  "lng" : 54.894743
               }
            },
            "location" : {
               "lat" : 24.9821547,
               "lng" : 55.402868
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 25.3538421,
                  "lng" : 56.2046836
               },
               "southwest" : {
                  "lat" : 24.605052,
                  "lng" : 54.894743
               }
            }
         },
         "place_id" : "ChIJRcbZaklDXz4R6SkAK7_QznQ",
         "types" : [ "administrative_area_level_1", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "الإمارات العربية المتحدة",
               "short_name" : "AE",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "الإمارات العربية المتحدة",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 26.0765,
                  "lng" : 56.4395001
               },
               "southwest" : {
                  "lat" : 22.6315138,
                  "lng" : 51.4723
               }
            },
            "location" : {
               "lat" : 23.424076,
               "lng" : 53.847818
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 26.0765,
                  "lng" : 56.4395001
               },
               "southwest" : {
                  "lat" : 22.6315138,
                  "lng" : 51.4723
               }
            }
         },
         "place_id" : "ChIJvRKrsd9IXj4RpwoIwFYv0zM",
         "types" : [ "country", "political" ]
      }
   ],
   "status" : "OK"
}

推荐答案

嗨Jassim Rahma,

Hi Jassim Rahma,

感谢您发布此处。

根据您的文件,我创建了一个JSON文件来存储它。如果你想获得第一个address_components的第三个long_time值Bur Dubai,请尝试下面的代码。

According to your file, I create a JSON file to store it. And if you want to get the third long_time value Bur Dubai of first address_components, please try the code below.

  string path = @"D:\Visual Studio 2017\Projects\ConsoleApp\ConsoleApp\get data from JSON.json";
            using (StreamReader sr = new StreamReader(path))
            {
                string data = sr.ReadToEnd();
                JObject obj = JObject.Parse(data);
                string longTime = (string)obj["results"][0]["address_components"][2]["long_name"];
            }

我认为你把[0],[2]置于错误的位置。

I think you put the [0],[2] in wrong place.

最好的问候,

Wendy 

Wendy 


这篇关于如何从Google json阅读城市的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆