JSON 解析时出错

JSON error during parsing

一直在寻找有关如何解析此 json 的信息,但我尝试的一切都失败了。

我正在尝试使用此代码解析 JSON:

var client = new WebClient();
client.Headers.Add("User-Agent", "Nobody");
var response = client.DownloadString(new Uri("https://api.worldoftanks.ru/wot/encyclopedia/tanks/?application_id=demo"));
Response asd = JsonConvert.DeserializeObject<Response>(response);

我还在不同的文件中设置了这些类:

public class Response
{
    public string status { get; set; }
    public int count { get; set; }
    public List<Tank> data { get; set; }
}

public class Tank
{
    public string nation_i18n { get; set; }
    public string name { get; set; }
    public int level { get; set; }
    public string image { get; set; }
    public string image_small { get; set; }
    public string nation { get; set; }
    public bool is_premium { get; set; }
    public string type_i18n { get; set; }
    public string contour_image { get; set; }
    public string short_name_i18n { get; set; }
    public string name_i18n { get; set; }
    public string type { get; set; }
    public int tank_id { get; set; }
}

它们与 URL 返回的数据相同(请打开它,您将看到它是如何构建的)

我认为出错的一件事是,使用响应的 "data" 标签而不是每个坦克的标签 "Tank" 他们实际上将其命名为单独的 ID。 (再次请参阅 URL 示例)

有人可以帮我解决这个问题吗? 目前我收到错误:

An unhandled exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.dll Additional information: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[WotApp.Classes.Tank]' 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 'data.1', line 1, position 39.

希望得到你们的帮助。我多年来一直坚持这个:(

也在寻找答案并进行控制台应用程序测试,@Alexander 和@dotctor 的评论实际上是正确的答案;)因此您的 class 必须如下所示:

public class Response
{
  public string status { get; set; }
  public int count { get; set; }
  public Dictionary<String, Tank> data { get; set; }
}

Here is another SO question 处理完全相同的问题。

还有我的程序清单:

namespace SO27839862
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                WebClient client = new WebClient();
                client.Headers.Add("User-Agent", "Nobody");

                String response = client.DownloadString(new Uri("https://api.worldoftanks.ru/wot/encyclopedia/tanks/?application_id=demo"));
                Response asd = JsonConvert.DeserializeObject<Response>(response);
            }
            catch (Exception ex)
            {

            }
        }
    }

    public class Response
    {
        public string status { get; set; }
        public int count { get; set; }
        public Dictionary<String, Tank> data { get; set; }
    }

    public class Tank
    {
        public string nation_i18n { get; set; }
        public string name { get; set; }
        public int level { get; set; }
        public string image { get; set; }
        public string image_small { get; set; }
        public string nation { get; set; }
        public bool is_premium { get; set; }
        public string type_i18n { get; set; }
        public string contour_image { get; set; }
        public string short_name_i18n { get; set; }
        public string name_i18n { get; set; }
        public string type { get; set; }
        public int tank_id { get; set; }
    }
}