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示例) 有人可以帮帮我吗?
希望你的帮助.多年来我一直坚持这个:( 解决方法
也正在寻找和控制台应用程序测试的答案和@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; } } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |