2

簡単なクラスを作成し、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 から返される 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"
}

私はここで簡単に行方不明です。

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

これを少し単純化できることに注意してください: 生成されたNortheastおよびSouthwestクラスは と同じであるため、クラス内で使用されている場所にLocation置き換えることができます。LocationViewport

于 2013-08-02T15:50:01.317 に答える