c# - 解析时出现 JSON 错误

标签 c# json

一直在寻找有关如何解析此 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 返回的数据相同(请打开它,您将看到它是如何构建的)

我认为出错的一件事是,使用响应的“数据”标签,而不是每个坦克都有一个“坦克”标签,他们实际上将其命名为单独的 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处理完全相同的问题。

还有我的程序 list :

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

关于c# - 解析时出现 JSON 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27839862/

相关文章:

c# - 检查整数是否== null

c# - 在 c sharp 类中修改 HTML 文件

c# - 启动和即发即弃异步调用的正确方法?

javascript - 函数内部构建的对象数组存在类型问题

json - 检索并使用Volley错误HTTP响应代码和开关大小写来处理错误

c# - Windows 窗体中的控件不显示工具提示气球

c# - Java 的 "synchronized"和 C 的 #'s "锁有区别吗?

json - 将 JSON 结果读取为字典数组

java - 如何在 java 中访问 WrappedArray 的 scala.collection.mutable.WrappedArray 中的值

java - 如何在 Web 服务响应中将 json 响应作为 block 返回?