gpt4 book ai didi

c# - 使用 Json.Net 解析 Google Maps Geocode API

转载 作者:行者123 更新时间:2023-12-02 08:39:19 25 4
gpt4 key购买 nike

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

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

最佳答案

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

关于c# - 使用 Json.Net 解析 Google Maps Geocode API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18021084/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com