从Json中挑选值

本文关键字:挑选 Json | 更新日期: 2023-09-27 18:14:16

我有这个代码与GoogleMapsApi工作:

 public void GoogleGeoCode(string address)
        {
            address = "Stockholm";
            string url = "http://maps.googleapis.com/maps/api/geocode/json?sensor=true&address=";
            dynamic googleResults = new Uri(url + address).GetDynamicJsonObject();
            //Code that lets me pick out values from googleresults
        }

googlerresults包含值(经度/纬度),但我怎么能把这些出来,并使用为其他东西?我在类似的情况下使用了这种方法,但在这种情况下它不是有效的代码:

 JObject o = JObject.Parse(googleresult);
 string name = (string)o.SelectToken("longitude");
编辑:

这似乎选择放置经度和纬度:

public void GoogleGeoCode(string address)
        {
            address = "Stockholm";
            string url = "http://maps.googleapis.com/maps/api/geocode/json?sensor=true&address=";
            dynamic googleResults = new Uri(url + address).GetDynamicJsonObject();

            foreach (var result in googleResults.results)
            {
                double lng = result.geometry.location.lng;
                double  lat  = result.geometry.location.lat;
            }
            //Do something else...
        }

从Json中挑选值

您的googlerresults不是字符串。它是一个动态的jason对象。看看这个

代码从这里:寻找一个REST与JSON客户端库

public static void GoogleGeoCode(string address)
{
    string url = "http://maps.googleapis.com/maps/api/geocode/json?sensor=true&address=";
    dynamic googleResults = new Uri(url + address).GetDynamicJsonObject();
    foreach (var result in googleResults.results)
    {
        Console.WriteLine("[" + result.geometry.location.lat + "," + 
                            result.geometry.location.lng + "] " + 
                            result.formatted_address);
    }
}