简体   繁体   English

解析期间出现JSON错误

[英]JSON error during parsing

Been looking for information on how to parse this json however everything I tried fails. 一直在寻找有关如何解析此json的信息,但是我尝试的所有操作均失败。

I am trying to parse JSON using this code: 我正在尝试使用以下代码解析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);

And I also have these classes set-up in a different file: 我还在其他文件中设置了这些类:

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

They are identical to data returned by the URL (please open it and you will see how it's built) 它们与URL返回的数据相同(请打开它,您将看到它的构建方式)

One thing I think is going wrong is that withing the "data" tag of the response instead of having a tag of "Tank" for each tank they actually named it as individual IDs. 我认为出错的是,在响应中使用“数据”标签,而不是为每个实际将其命名为单独ID的坦克添加“ Tank”标签。 (again, please see URL for example) (同样,请参见URL)

Can anybody please help me with this? 有人可以帮我吗? At the moment I am getting error: 目前,我遇到了错误:

An unhandled exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.dll Additional information: Cannot deserialize the current JSON object (eg {"name":"value"}) into type 'System.Collections.Generic.List`1[WotApp.Classes.Tank]' because the type requires a JSON array (eg [1,2,3]) to deserialize correctly. Newtonsoft.Json.dll中发生了类型为'Newtonsoft.Json.JsonSerializationException'的未处理异常。其他信息:无法将当前JSON对象(例如{“ name”:“ value”})反序列化为类型'System.Collections.Generic.List。 “ 1 [WotApp.Classes.Tank]”,因为该类型需要JSON数组(例如[1,2,3])才能正确反序列化。 To fix this error either change the JSON to a JSON array (eg [1,2,3]) or change the deserialized type so that it is a normal .NET type (eg not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. 要解决此错误,可以将JSON更改为JSON数组(例如[1,2,3]),也可以更改反序列化类型,使其成为普通的.NET类型(例如,不像整数这样的原始类型,也不像这样的集合类型。数组或列表),可以从JSON对象反序列化。 JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. 还可以将JsonObjectAttribute添加到类型中,以强制其从JSON对象反序列化。 Path 'data.1', line 1, position 39. 路径“ data.1”,第1行,位置39。

Hope for your help guys. 希望对您有所帮助。 I've been stuck with this for ages :( 我已经坚持了很长时间:(

Was also looking and console-app testing for an answer and the comments of @Alexander and @dotctor are actually right answers ;) So your class will have to look like this: 还正在寻找答案,并且正在控制台应用程序测试中,@ Alexander和@dotctor的注释实际上是正确的答案;)因此,您的类必须像这样:

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 dealing with exactly the same problem. 这是另一个处理完全相同的问题的SO问题。

And my program listing: 而我的程序清单:

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

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM