使用Json.Net解析Google Maps Geocode API

时间:2013-08-02 15:44:23

标签: c# google-maps json.net

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

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

    public class Geometry
    {
        public Location location { get; set; }
    }

    public class Json
    {
        public Results results { get; set; }
    }

    public class Results
    {
        public Json json { get; set; }
        public Geometry geometry { get; set; }
    }

    public class Query
    {
        public Results results { get; set; }
    }

    public class RootObject
    {
        public Query query { get; set; }
    }

以下是从Google返回的内容的示例:

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "1600",
               "short_name" : "1600",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "Amphitheatre Parkway",
               "short_name" : "Amphitheatre Pkwy",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Mountain View",
               "short_name" : "Mountain View",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Santa Clara",
               "short_name" : "Santa Clara",
               "types" : [ "administrative_area_level_2", "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" ]
            }
         ],
         "formatted_address" : "1600 Amphitheatre Parkway, Mountain View, CA 94043, USA",
         "geometry" : {
            "location" : {
               "lat" : 37.42244920,
               "lng" : -122.08506440
            },
            "location_type" : "ROOFTOP",
            "viewport" : {
               "northeast" : {
                  "lat" : 37.42379818029150,
                  "lng" : -122.0837154197085
               },
               "southwest" : {
                  "lat" : 37.42110021970850,
                  "lng" : -122.0864133802915
               }
            }
         },
         "types" : [ "street_address" ]
      }
   ],
   "status" : "OK"
}

我在这里很简单,我知道,有人吗?

2 个答案:

答案 0 :(得分:5)

尝试使用json2csharp.com生成课程。使用此工具,类结构如下所示:

public class AddressComponent
{
    public string long_name { get; set; }
    public string short_name { get; set; }
    public List<string> types { get; set; }
}

public class Location
{
    public double lat { get; set; }
    public double lng { get; set; }
}

public class Northeast
{
    public double lat { get; set; }
    public double lng { get; set; }
}

public class Southwest
{
    public double lat { get; set; }
    public double lng { get; set; }
}

public class Viewport
{
    public Northeast northeast { get; set; }
    public Southwest southwest { get; set; }
}

public class Geometry
{
    public Location location { get; set; }
    public string location_type { get; set; }
    public Viewport viewport { get; set; }
}

public class Result
{
    public List<AddressComponent> address_components { get; set; }
    public string formatted_address { get; set; }
    public Geometry geometry { get; set; }
    public List<string> types { get; set; }
}

public class RootObject
{
    public List<Result> results { get; set; }
    public string status { get; set; }
}

请注意,您可以稍微简化一下:生成的NortheastSouthwest类与Location相同,因此您可以使用Location替换它们在Viewport类中。

答案 1 :(得分:0)

在较新版本的Visual Studio中,您可以选择D.instance_methods #=> [:hiya, :what_if?] (A.instance_methods & D.instance_methods).each { |m| D.send(:remove_method, m) } #=> [:hiya] D.instance_methods #=> [:what_if?] C.instance_methods.include?(:hiya) #=> true a.hiya("Lois") ho Lois 来创建类。

以下是使用最新版本的Google Maps API returned JSON example创建的结果:

Edit > Paste Special > Paste JSON as Classes

您可以将Rootobject重命名为您需要的任何内容。

在同一菜单中还有一个XML选项,可以使用XML执行相同的操作。