Newtonsoft.Json.JsonSerializationException: '无法反序列化当前 JSON 数组
Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON array
我是 API 的新手,在过去的几天里,我一直在练习使用来自 github 等的预制 API。现在我决定尝试创建自己的冠状病毒追踪应用程序(非常原创)。我已 运行 进入标题问题,但尚未在网上找到有关如何修复该问题的解决方案。我想我正在尝试接收的 JSON (https://api.covid19api.com/live/country/germany) 是一个数组,我无法让它工作。我在非数组 JSON (reddit's) 上尝试过同样的事情,它就像一个魅力。所有代码和 类 都粘贴在下面,感谢所有花时间阅读本文并决定提供帮助的人。
Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'Covid.Api.CovidStats' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.
Path '', line 1, position 1.'
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
namespace Covid.Api
{
public class CovidClient
{
public async Task<CovidStats> GetByCountryLiveStats(string country,DateTime startDate,DateTime endDate)
{
var url = $"https://api.covid19api.com/country/{country}/status/confirmed/live?from={startDate}&to={endDate}";
var client = new HttpClient();
var response = await client.GetStringAsync(url);
return JsonConvert.DeserializeObject<CovidStats>(response);
}
}
public class CovidStats
{
[JsonProperty("Country")]
public string Country { get; set; }
[JsonProperty("Cases")]
public int Cases { get; set; }
[JsonProperty("Status")]
public string Status { get; set; }
[JsonProperty("Date")]
public DateTime date { get; set; }
}
public class CovidList
{
List<CovidStats> lista { get; set; }
}
}
API return 是一个 List 而不是单个对象,因此您需要更新反序列化行:
return JsonConvert.DeserializeObject<List<CovidStats>>(response);
更新:
为了完整性,您还需要更新方法的 return 类型。完整代码如下:
public static async Task<List<CovidStats>> GetByCountryLiveStats(string country, DateTime startDate, DateTime endDate)
{
var url = $"https://api.covid19api.com/country/{country}/status/confirmed/live?from= {startDate}&to={endDate}";
var client = new HttpClient();
var response = await client.GetStringAsync(url);
return JsonConvert.DeserializeObject<List<CovidStats>>(response);
}
试试这个
JsonConvert.DeserializeObject<CovidList>(response);
我是 API 的新手,在过去的几天里,我一直在练习使用来自 github 等的预制 API。现在我决定尝试创建自己的冠状病毒追踪应用程序(非常原创)。我已 运行 进入标题问题,但尚未在网上找到有关如何修复该问题的解决方案。我想我正在尝试接收的 JSON (https://api.covid19api.com/live/country/germany) 是一个数组,我无法让它工作。我在非数组 JSON (reddit's) 上尝试过同样的事情,它就像一个魅力。所有代码和 类 都粘贴在下面,感谢所有花时间阅读本文并决定提供帮助的人。
Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'Covid.Api.CovidStats' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly. To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array. Path '', line 1, position 1.'
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
namespace Covid.Api
{
public class CovidClient
{
public async Task<CovidStats> GetByCountryLiveStats(string country,DateTime startDate,DateTime endDate)
{
var url = $"https://api.covid19api.com/country/{country}/status/confirmed/live?from={startDate}&to={endDate}";
var client = new HttpClient();
var response = await client.GetStringAsync(url);
return JsonConvert.DeserializeObject<CovidStats>(response);
}
}
public class CovidStats
{
[JsonProperty("Country")]
public string Country { get; set; }
[JsonProperty("Cases")]
public int Cases { get; set; }
[JsonProperty("Status")]
public string Status { get; set; }
[JsonProperty("Date")]
public DateTime date { get; set; }
}
public class CovidList
{
List<CovidStats> lista { get; set; }
}
}
API return 是一个 List
return JsonConvert.DeserializeObject<List<CovidStats>>(response);
更新:
为了完整性,您还需要更新方法的 return 类型。完整代码如下:
public static async Task<List<CovidStats>> GetByCountryLiveStats(string country, DateTime startDate, DateTime endDate)
{
var url = $"https://api.covid19api.com/country/{country}/status/confirmed/live?from= {startDate}&to={endDate}";
var client = new HttpClient();
var response = await client.GetStringAsync(url);
return JsonConvert.DeserializeObject<List<CovidStats>>(response);
}
试试这个
JsonConvert.DeserializeObject<CovidList>(response);