locked
How to read city from Google json RRS feed

  • Question

  • Hi,

    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];
    }

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

    This is the result:

    {
       "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"
    }


    Thursday, May 17, 2018 5:21 PM

All replies

  • Hi Jassim Rahma,

    Thank you for posting here.

    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"];
                }

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

    Best Regards,

    Wendy 


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Sunday, May 20, 2018 3:52 PM