挑选出Json的价值

时间:2014-07-22 16:16:45

标签: c# json

我有使用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

        }

googleresults包含值(经度/纬度),但我怎样才能将这些值用于其他内容?我在类似的情况下使用了这种方法,但在这种情况下它不是有效的代码:

 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...

        }

1 个答案:

答案 0 :(得分:2)

您的Google搜索结果不是字符串。它是一个动态的json对象。试着看看这个。

此处的代码:Looking for a REST with JSON client library

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