使用 C# - ASP.NET Core MVC 反序列化 API Json 时出错
Error in Deserializing the API Json using C# - ASP.NET Core MVC
我正在尝试调用 API 并反序列化输出的 json。
单击我的视图时出现以下错误:
JsonSerializationException: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[MvcSpatial.Models.Weather+Root]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
Path 'type', line 2, position 9.*
我认为我正在使用 IEnumerable
,但我不知道这是否是问题的原因。有什么想法吗?
这是我的 类 使用 https://json2csharp.com/
public class weather
{
public class Symbol
{
[JsonProperty("value")]
public string Value { get; set; }
[JsonProperty("type")]
public string Type { get; set; }
}
public class Unit
{
[JsonProperty("label")]
public string Label { get; set; }
[JsonProperty("symbol")]
public Symbol Symbol { get; set; }
}
public class ObservedProperty
{
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("label")]
public string Label { get; set; }
}
public class MeasurementType
{
[JsonProperty("method")]
public string Method { get; set; }
[JsonProperty("period")]
public string Period { get; set; }
}
public class DewPoint
{
[JsonProperty("type")]
public string Type { get; set; }
[JsonProperty("description")]
public string Description { get; set; }
[JsonProperty("unit")]
public Unit Unit { get; set; }
[JsonProperty("observedProperty")]
public ObservedProperty ObservedProperty { get; set; }
[JsonProperty("measurementType")]
public MeasurementType MeasurementType { get; set; }
}
public class Parameters
{
[JsonProperty("Dew point")]
public DewPoint DewPoint { get; set; }
}
public class Properties
{
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("Dew point")]
public List<int> DewPoint { get; set; }
[JsonProperty("time")]
public List<DateTime> Time { get; set; }
}
public class Geometry
{
[JsonProperty("type")]
public string Type { get; set; }
[JsonProperty("coordinates")]
public List<double> Coordinates { get; set; }
}
public class Feature
{
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("type")]
public string Type { get; set; }
[JsonProperty("properties")]
public Properties Properties { get; set; }
[JsonProperty("geometry")]
public Geometry Geometry { get; set; }
}
public class Root
{
[JsonProperty("type")]
public string Type { get; set; }
[JsonProperty("parameters")]
public Parameters Parameters { get; set; }
[JsonProperty("bbox")]
public List<double> Bbox { get; set; }
[JsonProperty("features")]
public List<Feature> Features { get; set; }
}
}
我的控制器
public async Task<IActionResult> Weather()
{
List<Root> weatherlist = new List<Root>();
using (var httpClient = new HttpClient())
{
using (var response = await httpClient.GetAsync("http://labs.metoffice.gov.uk/edr/collections/obs_demo/radius?coords=POINT(-0.109421%2051.508558)&within=99&within-units=km¶meter-name=Dew%20point&datetime=2022-01-06T03:00Z/2022-01-06T05:00Z&crs=CRS84&f=GeoJSON"))
{
string apiResponse = await response.Content.ReadAsStringAsync();
weatherlist = JsonConvert.DeserializeObject<List<Root>>(apiResponse);
}
}
return View(weatherlist);
}
这里是 API 输出的 json
{
"type": "FeatureCollection",
"parameters": {
someThings there
}
},
"bbox": [
-0.123,
0.123,
0.123,
0.123
],
"features": [
{
"id": "EGWU",
"type": "Feature",
"properties": {
"name": "tt",
"Dew point": [
-5
],
"time": [
"2022-01-06T04:50:00Z"
]
},
"geometry": {
"type": "Point",
"coordinates": [
-0.417,
51.55
]
}
},
{
"id": "EGMC",
"type": "Feature",
"properties": {
"name": "SOUTHEND-ON-SEA",
"Dew point": [
-2
],
"time": [
"2022-01-06T04:50:00Z"
]
},
"geometry": {
"type": "Point",
"coordinates": [
0.7,
51.567
]
}
},
{
"id": "EGGW",
"type": "Feature",
"properties": {
"name": "LUTON AIRPORT",
"Dew point": [
-3,
-4
],
"time": [
"2022-01-06T04:50:00Z",
"2022-01-06T04:20:00Z"
]
},
"geometry": {
"type": "Point",
"coordinates": [
-0.367,
51.867
]
}
},
{
"id": "EGSS",
"type": "Feature",
"properties": {
"name": "STANSTED AIRPORT",
"Dew point": [
-4,
-3
],
"time": [
"2022-01-06T04:50:00Z",
"2022-01-06T04:20:00Z"
]
},
"geometry": {
"type": "Point",
"coordinates": [
0.217,
51.883
]
}
},
{
"id": "EGKK",
"type": "Feature",
"properties": {
"name": "LONDON/GATWICK A",
"Dew point": [
-4,
-5
],
"time": [
"2022-01-06T04:50:00Z",
"2022-01-06T04:20:00Z"
]
},
"geometry": {
"type": "Point",
"coordinates": [
-0.167,
51.133
]
}
},
{
"id": "EGLF",
"type": "Feature",
"properties": {
"name": "FARNBOROUGH",
"Dew point": [
-6,
-6
],
"time": [
"2022-01-06T04:50:00Z",
"2022-01-06T04:20:00Z"
]
},
"geometry": {
"type": "Point",
"coordinates": [
-0.767,
51.283
]
}
}
]
}
更新:这是我的看法
@Model IEnumerable<weather>
<table class="table table-sm table-striped table-bordered m-2">
<thead>
<tr>
<th>type</th>
<th>Bbox</th>
</tr>
</thead>
<tbody>
@foreach (var r in Model)
{
<tr>
<td>@r.type</td>
<td>@r.Bbox</td>
</tr>
}
</tbody>
</table>
试试这个,你的 json 根是一个对象,而不是数组所以你必须使用 Root 而不是 List
Root featureCollection=null;
string apiResponse = await response.Content.ReadAsStringAsync();
featureCollection = JsonConvert.DeserializeObject<Root>(apiResponse);
List<Feature> features=featureCollection.Feautures;
return View(features);
查看
@model List<Feature>
<table class="table table-sm table-striped table-bordered m-2">
<thead>
<tr>
<th>Feature</th>
<th>Property Name</th>
<th>Property DewPoint</th>
</tr>
</thead>
<tbody>
@foreach (var feature in Model)
{
@foreach (var property in feature.Properties)
{
<tr>
<td>@feature.Id</td>
<td>@property.Name</td>
<td>@property.DewPoint[0] : @property.DewPoint[1] </td>
</tr>
}
}
</tbody>
</table>
我正在尝试调用 API 并反序列化输出的 json。 单击我的视图时出现以下错误:
JsonSerializationException: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[MvcSpatial.Models.Weather+Root]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. Path 'type', line 2, position 9.*
我认为我正在使用 IEnumerable
,但我不知道这是否是问题的原因。有什么想法吗?
这是我的 类 使用 https://json2csharp.com/
public class weather
{
public class Symbol
{
[JsonProperty("value")]
public string Value { get; set; }
[JsonProperty("type")]
public string Type { get; set; }
}
public class Unit
{
[JsonProperty("label")]
public string Label { get; set; }
[JsonProperty("symbol")]
public Symbol Symbol { get; set; }
}
public class ObservedProperty
{
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("label")]
public string Label { get; set; }
}
public class MeasurementType
{
[JsonProperty("method")]
public string Method { get; set; }
[JsonProperty("period")]
public string Period { get; set; }
}
public class DewPoint
{
[JsonProperty("type")]
public string Type { get; set; }
[JsonProperty("description")]
public string Description { get; set; }
[JsonProperty("unit")]
public Unit Unit { get; set; }
[JsonProperty("observedProperty")]
public ObservedProperty ObservedProperty { get; set; }
[JsonProperty("measurementType")]
public MeasurementType MeasurementType { get; set; }
}
public class Parameters
{
[JsonProperty("Dew point")]
public DewPoint DewPoint { get; set; }
}
public class Properties
{
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("Dew point")]
public List<int> DewPoint { get; set; }
[JsonProperty("time")]
public List<DateTime> Time { get; set; }
}
public class Geometry
{
[JsonProperty("type")]
public string Type { get; set; }
[JsonProperty("coordinates")]
public List<double> Coordinates { get; set; }
}
public class Feature
{
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("type")]
public string Type { get; set; }
[JsonProperty("properties")]
public Properties Properties { get; set; }
[JsonProperty("geometry")]
public Geometry Geometry { get; set; }
}
public class Root
{
[JsonProperty("type")]
public string Type { get; set; }
[JsonProperty("parameters")]
public Parameters Parameters { get; set; }
[JsonProperty("bbox")]
public List<double> Bbox { get; set; }
[JsonProperty("features")]
public List<Feature> Features { get; set; }
}
}
我的控制器
public async Task<IActionResult> Weather()
{
List<Root> weatherlist = new List<Root>();
using (var httpClient = new HttpClient())
{
using (var response = await httpClient.GetAsync("http://labs.metoffice.gov.uk/edr/collections/obs_demo/radius?coords=POINT(-0.109421%2051.508558)&within=99&within-units=km¶meter-name=Dew%20point&datetime=2022-01-06T03:00Z/2022-01-06T05:00Z&crs=CRS84&f=GeoJSON"))
{
string apiResponse = await response.Content.ReadAsStringAsync();
weatherlist = JsonConvert.DeserializeObject<List<Root>>(apiResponse);
}
}
return View(weatherlist);
}
这里是 API 输出的 json
{
"type": "FeatureCollection",
"parameters": {
someThings there
}
},
"bbox": [
-0.123,
0.123,
0.123,
0.123
],
"features": [
{
"id": "EGWU",
"type": "Feature",
"properties": {
"name": "tt",
"Dew point": [
-5
],
"time": [
"2022-01-06T04:50:00Z"
]
},
"geometry": {
"type": "Point",
"coordinates": [
-0.417,
51.55
]
}
},
{
"id": "EGMC",
"type": "Feature",
"properties": {
"name": "SOUTHEND-ON-SEA",
"Dew point": [
-2
],
"time": [
"2022-01-06T04:50:00Z"
]
},
"geometry": {
"type": "Point",
"coordinates": [
0.7,
51.567
]
}
},
{
"id": "EGGW",
"type": "Feature",
"properties": {
"name": "LUTON AIRPORT",
"Dew point": [
-3,
-4
],
"time": [
"2022-01-06T04:50:00Z",
"2022-01-06T04:20:00Z"
]
},
"geometry": {
"type": "Point",
"coordinates": [
-0.367,
51.867
]
}
},
{
"id": "EGSS",
"type": "Feature",
"properties": {
"name": "STANSTED AIRPORT",
"Dew point": [
-4,
-3
],
"time": [
"2022-01-06T04:50:00Z",
"2022-01-06T04:20:00Z"
]
},
"geometry": {
"type": "Point",
"coordinates": [
0.217,
51.883
]
}
},
{
"id": "EGKK",
"type": "Feature",
"properties": {
"name": "LONDON/GATWICK A",
"Dew point": [
-4,
-5
],
"time": [
"2022-01-06T04:50:00Z",
"2022-01-06T04:20:00Z"
]
},
"geometry": {
"type": "Point",
"coordinates": [
-0.167,
51.133
]
}
},
{
"id": "EGLF",
"type": "Feature",
"properties": {
"name": "FARNBOROUGH",
"Dew point": [
-6,
-6
],
"time": [
"2022-01-06T04:50:00Z",
"2022-01-06T04:20:00Z"
]
},
"geometry": {
"type": "Point",
"coordinates": [
-0.767,
51.283
]
}
}
]
}
更新:这是我的看法
@Model IEnumerable<weather>
<table class="table table-sm table-striped table-bordered m-2">
<thead>
<tr>
<th>type</th>
<th>Bbox</th>
</tr>
</thead>
<tbody>
@foreach (var r in Model)
{
<tr>
<td>@r.type</td>
<td>@r.Bbox</td>
</tr>
}
</tbody>
</table>
试试这个,你的 json 根是一个对象,而不是数组所以你必须使用 Root 而不是 List
Root featureCollection=null;
string apiResponse = await response.Content.ReadAsStringAsync();
featureCollection = JsonConvert.DeserializeObject<Root>(apiResponse);
List<Feature> features=featureCollection.Feautures;
return View(features);
查看
@model List<Feature>
<table class="table table-sm table-striped table-bordered m-2">
<thead>
<tr>
<th>Feature</th>
<th>Property Name</th>
<th>Property DewPoint</th>
</tr>
</thead>
<tbody>
@foreach (var feature in Model)
{
@foreach (var property in feature.Properties)
{
<tr>
<td>@feature.Id</td>
<td>@property.Name</td>
<td>@property.DewPoint[0] : @property.DewPoint[1] </td>
</tr>
}
}
</tbody>
</table>