【问题标题】:parse google maps geocode json response to object using Json.Net使用 Json.Net 解析对对象的谷歌地图地理编码 json 响应
【发布时间】:2010-06-08 20:41:58
【问题描述】:

我有一个数据库,其中包含我需要获取经纬度的地址,因此我想遍历它们并使用 Google 地理编码来更新我的数据库。我不知道如何解析 JSOn 结果以获得我需要的东西:

var address = "http://maps.google.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false";
var result = new System.Net.WebClient().DownloadString(address);
GoogleGeoCodeResponse test = JsonConvert.DeserializeObject<GoogleGeoCodeResponse>(result);

我认为我可以简单地构建一个快速类并使用 JSON.Net 反序列化结果,它有点工作,但我认为我在我的类结构上吹了它:

public  class GoogleGeoCodeResponse {

    public string status { get; set; }
    public geometry geometry { get; set; }

}

public class geometry {
    public string location_type { get; set; }
    public location location { get; set; }
}

public class location {
    public string lat {get;set;}
    public string lng {get;set;}
}

以下是 Google 返回的示例:

{
  "status": "OK",
  "results": [ {
    "types": [ "street_address" ],
    "formatted_address": "1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA",
    "address_components": [ {
      "long_name": "1600",
      "short_name": "1600",
      "types": [ "street_number" ]
    }, {
      "long_name": "Amphitheatre Pkwy",
      "short_name": "Amphitheatre Pkwy",
      "types": [ "route" ]
    }, {
      "long_name": "Mountain View",
      "short_name": "Mountain View",
      "types": [ "locality", "political" ]
    }, {
      "long_name": "California",
      "short_name": "CA",
      "types": [ "administrative_area_level_1", "political" ]
    }, {
      "long_name": "United States",
      "short_name": "US",
      "types": [ "country", "political" ]
    }, {
      "long_name": "94043",
      "short_name": "94043",
      "types": [ "postal_code" ]
    } ],
    "geometry": {
      "location": {
        "lat": 37.4219720,
        "lng": -122.0841430
      },
      "location_type": "ROOFTOP",
      "viewport": {
        "southwest": {
          "lat": 37.4188244,
          "lng": -122.0872906
        },
        "northeast": {
          "lat": 37.4251196,
          "lng": -122.0809954
        }
      }
    }
  } ]
}

我在这里缺少简单我知道的,有人吗?

【问题讨论】:

    标签: c# json google-maps


    【解决方案1】:

    我试过这个,做了一个简单的测试,它起作用了(添加结果和其他):

    public class GoogleGeoCodeResponse
    {
    
        public string status { get; set; }
        public results[] results { get; set; }
    
    }
    
    public class results
    {
        public string formatted_address { get; set; }
        public geometry geometry { get; set; }
        public string[] types { get; set; }
        public address_component[] address_components { get; set; }
    }
    
    public class geometry
    {
        public string location_type { get; set; }
        public location location { get; set; }
    }
    
    public class location
    {
        public string lat { get; set; }
        public string lng { get; set; }
    }
    
    public class address_component
    {
        public string long_name { get; set; }
        public string short_name { get; set; }
        public string[] types { get; set; }
    }
    

    【讨论】:

    • 您的数据结构适合我,但我更喜欢将 location.lat 和 location.lng 定义为小数。同样,为响应状态、结果类型和位置类型提供枚举更容易。 Json.NET 会将字符串反序列化为正确的枚举。
    • 这一行“public results[] results { get; set; }”解决了两个小时的障碍。我从来没有想过把另一个班级比结果班高一档。谢谢。
    • @antonio 酷,它只是第二个获取数据谢谢
    【解决方案2】:

    您可以使用动态对象而不是定义对象。

     public static dynamic GEOCodeAddress(String Address)
        {
            var address = String.Format("http://maps.google.com/maps/api/geocode/json?address={0}&sensor=false", Address.Replace(" ", "+"));
            var result = new System.Net.WebClient().DownloadString(address);
            JavaScriptSerializer jss = new JavaScriptSerializer();
            return jss.Deserialize<dynamic>(result);
        }
    

    【讨论】:

    • 命名空间是 -> System.Web.Extensions 中的 System.Web.Script.Serialization.JavaScriptSerializer(在 System.Web.Extensions.dll 中)
    • 我喜欢它的简单性。
    • 漂亮的工作.. 我试过了,在 .NET Framework 4(Microsoft.Csharp.dll) 上运行良好;而不能在 .NET Framework 3.5 中工作。有什么想法吗?
    • 谢谢大家,我感到温暖而模糊。 @user1671639 DownloadString() 随 .net 4.5 一起提供,您可以使用其他方法来使用其他“将网页作为字符串获取”方法来获取 Web 调用的结果...... JavaScriptSerializer 也是如此......但是也有 C# 的开源 JSON 实现。
    • @matt.j.crawford 谢谢。一个建议,请在您的回答中提及这一点。会对其他人有所帮助。
    【解决方案3】:

    我做过类似的事情 参考Google Geo Kit

    【讨论】:

    • 感谢您的示例。
    【解决方案4】:

    C# 对象代码 我添加了一些额外的类,不确定它们是否是 API 的新手,但我认为这可能对某人有所帮助。

    public class GoogleGeoCodeResponse
        {
            public results[] results { get; set; }
            public string status { get; set; }
    
        }
    
        public class results
        {
            public address_component[] address_components { get; set; }
            public string formatted_address { get; set; }
            public geometry geometry { get; set; }
            public string[] types { get; set; }
        }
    
        public class address_component
        {
            String long_name { get; set; }
            String short_name { get; set; }
            String types { get; set; }
    
        }
    
        public class geometry
        {
            public bounds bounds { get; set; }
            public location location { get; set; }
            public string location_type { get; set; }
            public viewport viewport { get; set; }
        }
    
        public class location
        {
            public string lat { get; set; }
            public string lng { get; set; }
        }
    
        public class viewport
        {
            public northeast northeast { get; set; }
            public southwest southwest { get; set; }
        }
    
        public class bounds
        {
            public northeast northeast { get; set; }
        }
    
        public class northeast
        {
            public string lat { get; set; }
            public string lng { get; set; }
        }
    
        public class southwest
        {
            public string lat { get; set; }
            public string lng { get; set; }
        }
    

    【讨论】:

      【解决方案5】:

      感谢上面的 JEuvin,我能够轻松地从 XML 切换到 Json,只需对上面的代码进行一些修改(特别是将 lat 和 lng 更改为十进制或双精度),还必须将 address_components.types 更改为 string[]让它为我工作。然后我进行了一些重构,以便可以互换地从 XML 或 Json 反序列化相同的类。

      也许这对某人也有帮助...

      using System;
      using System.Xml.Serialization;
      
      [Serializable]
      [XmlType(AnonymousType = true)]
      [XmlRoot(Namespace = "", IsNullable = false)]
      public class GeocodeResponse
      {
          public GeocodeResponse()
          {
          }
      
          [XmlElement("result")]
      
          public results[] results { get; set; }
      
          public string status { get; set; }
      }
      
      [XmlType(AnonymousType = true)]
      public class results
      {
          public results()
          {
          }
      
          [XmlElement("address_component")]
      
          public address_component[] address_components { get; set; }
      
          public string formatted_address { get; set; }
      
          public geometry geometry { get; set; }
      
          [XmlElement("type")]
          public string[] types { get; set; }
      
          public string[] postcode_localities { get; set; }
      
          public bool partial_match { get; set; }
      
          public string place_id { get; set; }
      }
      
      [XmlType(AnonymousType = true)]
      public class address_component
      {
          public address_component()
          {
          }
      
          public string long_name { get; set; }
      
          public string short_name { get; set; }
      
          [XmlElement("type")]
          public string[] types { get; set; }
      }
      
      [XmlType(AnonymousType = true)]
      public class geometry
      {
          public geometry()
          {
          }
      
          public bounds bounds { get; set; }
      
          public location location { get; set; }
      
          public string location_type { get; set; }
      
          public viewport viewport { get; set; }
      }
      
      [XmlType(AnonymousType = true)]
      public class location
      {
          public location()
          {
          }
      
          public double lat { get; set; }
      
          public double lng { get; set; }
      }
      
      [XmlType(AnonymousType = true)]
      public class viewport
      {
          public viewport()
          {
          }
      
          public northeast northeast { get; set; }
      
          public southwest southwest { get; set; }
      }
      
      [XmlType(AnonymousType = true)]
      public class bounds
      {
          public bounds()
          {
          }
      
          public northeast northeast { get; set; }
      }
      
      [XmlType(AnonymousType = true)]
      public class northeast
      {
          public northeast()
          {
          }
      
          public double lat { get; set; }
      
          public double lng { get; set; }
      }
      
      [XmlType(AnonymousType = true)]
      public class southwest
      {
          public southwest()
          {
          }
      
          public double lat { get; set; }
      
          public double lng { get; set; }
      }
      

      (编辑添加了 postcode_localities 和 partial_match 属性)

      【讨论】:

      • 确保你也添加了一些 JSON 属性。它有助于js操作。我个人不喜欢使用 XML。
      【解决方案6】:

      只是为了使用更简单的方法来处理所需内容的更新,您需要做的就是使用以下内容:

      var result = new System.Net.WebClient().DownloadString( <<ENTER ADDRESS URL HERE>> );
      dynamic geo = JsonConvert.DeserializeObject(result);
      

      然后您可以通过以下方式访问纬度:geo.results[0].geometry.location.lat

      【讨论】:

        【解决方案7】:

        确保类是可序列化的,允许为空

        [Serializable]
        [XmlType(AnonymousType = true)]
        [XmlRoot(Namespace = "", IsNullable = true)]
        public class GeocodeResponse
        {
            public GeocodeResponse()
            {
              // can be empty or you can initiate the properties here
            }
        
            [XmlElement("location ")]
            [Display(Name = "location ")]
            // add json attributes as well
            public location  location { get; set; }
        
            public string status { get; set; }
          }
        

        【讨论】:

          猜你喜欢
          • 2011-09-28
          • 1970-01-01
          • 2014-04-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-07-30
          相关资源
          最近更新 更多