分析谷歌地图地理代码API使用Json.Net

本文关键字:API 使用 Json Net 代码 谷歌地图 | 更新日期: 2023-09-27 18:26:09

我以为我可以简单地构建一个快速类,并使用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; }
    }

以下是从谷歌返回的get示例:

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

我错过了这里的简单我知道,有人吗?

分析谷歌地图地理代码API使用Json.Net

尝试使用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类中使用。

在Visual Studio的较新版本中,可以选择Edit > Paste Special > Paste JSON as Classes来创建类。

以下是使用最新版本的Google Maps API返回的JSON创建的结果示例:

public class Rootobject
{
    public Result[] results { get; set; }
    public string status { get; set; }
}
public class Result
{
    public Address_Components[] address_components { get; set; }
    public string formatted_address { get; set; }
    public Geometry geometry { get; set; }
    public string place_id { get; set; }
    public string[] types { get; set; }
}
public class Geometry
{
    public Location location { get; set; }
    public string location_type { get; set; }
    public Viewport viewport { get; set; }
}
public class Location
{
    public float lat { get; set; }
    public float lng { get; set; }
}
public class Viewport
{
    public Northeast northeast { get; set; }
    public Southwest southwest { get; set; }
}
public class Northeast
{
    public float lat { get; set; }
    public float lng { get; set; }
}
public class Southwest
{
    public float lat { get; set; }
    public float lng { get; set; }
}
public class Address_Components
{
    public string long_name { get; set; }
    public string short_name { get; set; }
    public string[] types { get; set; }
}

你可以将Rootobject重命名为你需要的任何东西。

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