简体   繁体   English

Newtonsoft.Json.JsonSerializationException: '无法反序列化当前的 JSON 数组

[英]Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON array

I am new to APIs and for the past couple of days i have been practicing using pre made ones from github and such.我是 API 的新手,过去几天我一直在练习使用来自 github 等的预制 API。 Now i have decided to try and create my own Coronavirus Tracker App (quite original).现在我决定尝试创建我自己的冠状病毒追踪器应用程序(非常原始)。 I have ran into the titled problem and have not found a solution online on how to fix it.我遇到了标题问题,但没有在网上找到有关如何修复它的解决方案。 I guess the JSON I am trying to recieve ( https://api.covid19api.com/live/country/germany ) is an array and i can not get it to work.我想我试图接收的 JSON ( https://api.covid19api.com/live/country/germany ) 是一个数组,我无法让它工作。 I have tried the same thing on a non array JSON (reddit's) and it works like a charm.我在非数组 JSON(reddit 的)上尝试过同样的事情,它就像一个魅力。 All of the code and classes are pasted below and thank you to anyone that takes the time to read this and decides to help.所有代码和类都粘贴在下面,感谢任何花时间阅读本文并决定提供帮助的人。

Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON array (eg [1,2,3]) into type 'Covid.Api.CovidStats' because the type requires a JSON object (eg {"name":"value"}) to deserialize correctly. Newtonsoft.Json.JsonSerializationException: '无法将当前 JSON 数组(例如 [1,2,3])反序列化为类型 'Covid.Api.CovidStats',因为该类型需要一个 JSON 对象(例如 {"name":"value"} ) 以正确反序列化。 To fix this error either change the JSON to a JSON object (eg {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (eg ICollection, IList) like List that can be deserialized from a JSON array.要修复此错误,请将 JSON 更改为 JSON 对象(例如 {"name":"value"})或将反序列化类型更改为数组或实现集合接口的类型(例如 ICollection、IList),例如可以从 JSON 数组反序列化。 JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array. JsonArrayAttribute 也可以添加到类型以强制它从 JSON 数组反序列化。 Path '', line 1, position 1.'路径 '',第 1 行,位置 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; }
}


}

The API returns a List<T> not a single object so you need to update the Deserialize line: API 返回一个 List<T> 而不是单个对象,因此您需要更新 Deserialize 行:

return JsonConvert.DeserializeObject<List<CovidStats>>(response);

UPDATE:更新:

For completeness, you will also need to update the return type of the method.为了完整起见,您还需要更新方法的返回类型。 Full code below:完整代码如下:

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Newtonsoft.Json.JsonSerializationException:&#39;无法反序列化当前JSON对象 - Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object Newtonsoft.Json.JsonSerializationException:无法反序列化当前的JSON对象 - Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON object Newtonsoft.Json.JsonSerializationException: '无法反序列化当前的 JSON object (例如 {"name":"value"}) - Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object (e.g. {"name":"value"}) Newtonsoft.Json.JsonSerializationException - Newtonsoft.Json.JsonSerializationException 错误:Newtonsoft.Json.JsonSerializationException - Error: Newtonsoft.Json.JsonSerializationException Xamarin Newtonsoft.Json.JsonSerializationException: - Xamarin Newtonsoft.Json.JsonSerializationException: 尝试使用模型反序列化时出现 Newtonsoft.Json.JsonSerializationException 问题 - Newtonsoft.Json.JsonSerializationException problem while trying to deserialize with a model 用户代码未处理Newtonsoft.Json.JsonSerializationException - Newtonsoft.Json.JsonSerializationException was unhandled by user code Twitterizer TwittterTimeline NewtonSoft.JSON.JsonSerializationException问题 - Twitterizer TwittterTimeline NewtonSoft.JSON.JsonSerializationException problems 如何处理Newtonsoft.Json.JsonSerializationException? - How to handle Newtonsoft.Json.JsonSerializationException?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM