加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 解析期间出现JSON错误

发布时间:2020-12-15 22:10:35 所属栏目:百科 来源:网络整理
导读:一直在寻找有关如何解析这个json的信息,但是我尝试的一切都失败了. 我试图使用以下代码解析JSON: var client = new WebClient();client.Headers.Add("User-Agent","Nobody");var response = client.DownloadString(new Uri("https://api.worldoftanks.ru/wo
一直在寻找有关如何解析这个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,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的评论实际上是正确的答案;)所以你的课将必须看起来像这样:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读