【发布时间】:2022-07-08 14:46:18
【问题描述】:
我是论坛的新手,我有一个问题。
我正在尝试使用 Newtonsoft 反序列化 NASA API 的 Neo Feed,但出现此错误
Newtonsoft.Json.JsonSerializationException:无法将当前 JSON 对象(例如 {"name":"value"})反序列化为类型 'System.Collections.Generic.IEnumerable
1[NasaApi.Models.Near_Earth_Objects]' 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<T>) 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 'links', line 1, position 9. at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent) at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType) at Newtonsoft.Json.JsonSerializer.Deserialize(JsonReader reader, Type objectType) at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings) at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings) at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value) at NasaApi.Services.NearEarthObjectService.GetAllNeos() in C:\Users\santanitaxx1050\Desktop\NasaApi\NasaApi\Services\NearEarthObjectService.cs:line 18 at lambda_method5(Closure , Object ) at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.AwaitableObjectResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Awaited|12_0(ControllerActionInvoker invoker, ValueTask1 actionResultValueTask) 在 Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.g__Awaited|10_0(ControllerActionInvoker 调用者,任务 lastTask,下一个状态,作用域范围,对象状态,布尔 isCompleted) 在 Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed 上下文) 在 Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(状态&下一个,范围&范围,对象&状态,布尔& isCompleted) 在 Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker. g__Awaited|13_0(ControllerActionInvoker 调用程序,任务 lastTask,下一个状态,作用域范围,对象状态,布尔 isCompleted) 在 Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker. g__Awaited|20_0(ResourceInvoker 调用程序,任务 lastTask,下一个状态,作用域范围,对象状态,布尔 isCompleted) 在 Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker. g__Awaited|17_0(ResourceInvoker 调用程序,任务任务,IDisposable 范围) 在 Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker. g__Awaited|17_0(ResourceInvoker 调用程序,任务任务,IDisposable 范围) 在 Microsoft.AspNetCore.Routing.EndpointMiddleware. g__AwaitRequestTask|6_0(端点端点、任务 requestTask、ILogger 记录器) 在 Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext 上下文) 在 Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext) 在 Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext,ISwaggerProvider swaggerProvider) 在 Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext 上下文) 标题
接受:/ 主机:本地主机:7008 用户代理:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.79 Safari/537.36 :方法:获取 接受编码:gzip、deflate、br 接受语言:es-ES,es;q=0.9 缓存控制:无缓存 邮递员令牌:ec30b624-b8b6-770d-57ce-4b6dcda1ffc2 秒-gpc:1 sec-fetch-site: 无 秒取模式:cors sec-fetch-dest: 空
我试过了
public async Task<IEnumerable<Near_Earth_Objects>> GetAllNeos()
{
var json = await _httpClient.GetStringAsync($"feed?start_date=2021-11-07&end_date=2021-11-10&api_key=Na1sKwJGK1HVeOF4Yx8aLNp4u8ygT5GSSMF26HQ2");
return JsonConvert.DeserializeObject<IEnumerable<Near_Earth_Objects>>(json);
}
有了这个JSON
{
"links": {
"next": "http://www.neowsapp.com/rest/v1/feed?start_date=2021-12-12&end_date=2021-12-15&detailed=false&api_key=Na1sKwJGK1HVeOF4Yx8aLNp4u8ygT5GSSMF26HQ2",
"prev": "http://www.neowsapp.com/rest/v1/feed?start_date=2021-12-06&end_date=2021-12-09&detailed=false&api_key=Na1sKwJGK1HVeOF4Yx8aLNp4u8ygT5GSSMF26HQ2",
"self": "http://www.neowsapp.com/rest/v1/feed?start_date=2021-12-09&end_date=2021-12-12&detailed=false&api_key=Na1sKwJGK1HVeOF4Yx8aLNp4u8ygT5GSSMF26HQ2"
},
"element_count": 76,
"near_earth_objects": {
"2021-12-12": [
{
"links": {
"self": "http://www.neowsapp.com/rest/v1/neo/2004341?api_key=Na1sKwJGK1HVeOF4Yx8aLNp4u8ygT5GSSMF26HQ2"
},
"id": "2004341",
"neo_reference_id": "2004341",
"name": "4341 Poseidon (1987 KF)",
"nasa_jpl_url": "http://ssd.jpl.nasa.gov/sbdb.cgi?sstr=2004341",
"absolute_magnitude_h": 16.05,
"estimated_diameter": {
"kilometers": {
"estimated_diameter_min": 1.6389095149,
"estimated_diameter_max": 3.6647130844
},
"meters": {
"estimated_diameter_min": 1638.9095149478,
"estimated_diameter_max": 3664.7130843945
},
"miles": {
"estimated_diameter_min": 1.0183708442,
"estimated_diameter_max": 2.277146434
},
"feet": {
"estimated_diameter_min": 5376.9998930214,
"estimated_diameter_max": 12023.337275805
}
},
"is_potentially_hazardous_asteroid": false,
"close_approach_data": [
{
"close_approach_date": "2021-12-12",
"close_approach_date_full": "2021-Dec-12 13:35",
"epoch_date_close_approach": 1639316100000,
"relative_velocity": {
"kilometers_per_second": "17.8282207618",
"kilometers_per_hour": "64181.5947426121",
"miles_per_hour": "39879.9470221525"
},
"miss_distance": {
"astronomical": "0.3316696597",
"lunar": "129.0194976233",
"kilometers": "49617074.634744839",
"miles": "30830620.5431592182"
},
"orbiting_body": "Earth"
}
],
"is_sentry_object": false
},
制作这个模型
public class Near_Earth_Objects
{
[JsonProperty("id")]
public int Id { get; set; }
[JsonProperty("name")]
public string Nombre { get; set; }
[JsonProperty("estimated_diameter:kilometers:estimated_diameter_min")]
public double DiametroMin { get; set; }
[JsonProperty("estimated_diameter:kilometers:estimated_diameter_max")]
public double DiametroMax { get; set; }
[JsonProperty("close_approach_data:relative_velocity:kilometers_per_hour")]
public double Velocidad { get; set; }
[JsonProperty("close_approach_data: close_approach_date")]
public DateTime Fecha { get; set; }
[JsonProperty("close_approach_date: orbiting_body")]
public string Planeta { get; set; }
}
我的反序列化代码是这样的
public async Task<IEnumerable<Near_Earth_Objects>> GetAllNeos()
{
var json = await _httpClient.GetStringAsync($"feed?start_date=2021-11-07&end_date=2021-11-10&api_key=Na1sKwJGK1HVeOF4Yx8aLNp4u8ygT5GSSMF26HQ2");
return JsonConvert.DeserializeObject<IEnumerable<Near_Earth_Objects>>(json);
}
查看 JSON 响应在 POSTMAN 上试试这个:
谢谢大家!! 对不起,我的英语不好,我来自西班牙:)
【问题讨论】:
-
您是否直接反序列化为
Near_Earth_Object?反序列化 JSON 的代码是什么? -
现代时代没有必要手工制作 json 类。 Json to C# 内置于 Visual Studio(将 json 复制到剪贴板,编辑/粘贴特殊/粘贴 JSON 作为类),或使用众多在线转换器之一(允许更好的转换,类似于您在问题);
-
您需要反序列化 包含
near_earth_objects的根对象,顺便说一下,您应该像这样声明near_earth_objectsDictionary <DateTime, Near_Earth_Objects[]> near_earth_objects -
@Neil 实际上,有。该工具是一个非常粗糙的工具,可以轻松生成 10 个相同的类,而不是重复使用同一个类。这也是一个不寻常的情况,因为
near_earth_objects被用作字典。如果您确实尝试过Paste As JSON,您会看到结果每个日期使用一个属性而不是字典和多个相同的类,而不是重用例如链接。没有 JSON Schema 或 OpenAPI 规范,工具只能猜测 -
@PanagiotisKanavos 我有点同意,但它将提供一个可以手动调整的基本工作版本。 OP 问题似乎缺少该工具将创建的一些根项。
标签: c# json .net serialization json.net