【发布时间】:2017-09-08 15:50:38
【问题描述】:
此 json 来自 google API 的 HTTP 响应:
{
"results": [
{
"address_components": [
{
"long_name": "Europe",
"short_name": "Europe",
"types": [
"continent",
"establishment",
"natural_feature"
]
}
],
"formatted_address": "Europe",
"geometry": {
"bounds": {
"northeast": {
"lat": 82.1673907,
"lng": 74.3555001
},
"southwest": {
"lat": 34.5428,
"lng": -31.4647999
}
},
"location": {
"lat": 54.5259614,
"lng": 15.2551187
},
"location_type": "APPROXIMATE",
"viewport": {
"northeast": {
"lat": 65,
"lng": 55
},
"southwest": {
"lat": 34,
"lng": -11
}
}
},
"place_id": "ChIJhdqtz4aI7UYRefD8s-aZ73I",
"types": [
"continent",
"establishment",
"natural_feature"
]
}
],
"status": "OK"
}
我建立了一个列表类:
public class geocode {
public class AddressComponent
{
public string long_name { get; set; }
public string short_name { get; set; }
public List<string> types { 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 Bounds
{
public Northeast northeast { get; set; }
public Southwest southwest { get; set; }
}
public class Location
{
public double lat { get; set; }
public double lng { get; set; }
}
public class Northeast2
{
public int lat { get; set; }
public int lng { get; set; }
}
public class Southwest2
{
public int lat { get; set; }
public int lng { get; set; }
}
public class Viewport
{
public Northeast2 northeast { get; set; }
public Southwest2 southwest { get; set; }
}
public class Geometry
{
public Bounds bounds { get; set; }
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 string place_id { get; set; }
public List<string> types { get; set; }
}
public class RootObject
{
public List<Result> results { get; set; }
public string status { get; set; }
}
}
这是我得到的:
nombrePapa = nombrePapa.ToUpper();
var resultadoGeocoding = ggo.geocoding(nombrePapa);
//var result = JsonConvert.DeserializeObject<listas.geocode.Result>(resultadoGeocoding);
List<listas.geocode> lista = JsonConvert.DeserializeObject<List<listas.geocode>>(resultadoGeocoding);
但是这种反序列化让我坚持了下来:
无法将当前 JSON 对象(例如 {"name":"value"})反序列化为类型 'System.Collections.Generic.List`1[Models.listas+geocode]' 因为该类型需要 JSON 数组(例如[1,2,3]) 正确反序列化。
我错过了什么?
【问题讨论】: