2

I thought I could simply build a quick class and use JSON.Net to deserialize the result, and it is kind of working but I think I am blowing it on my class structure:

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

Here is a sample of what get's returned from 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"
}

I am missing simple here I know it, anyone?

4

2 回答 2

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,因此您可以将它们替换为LocationViewport类中使用它们的位置。

于 2013-08-02T15:50:01.317 回答
1

在较新版本的 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 执行相同的操作。

于 2016-04-22T18:48:44.557 回答