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 @R_426_4045@ion: 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.
希望你的帮助.多年来我一直坚持这个:(
解决方法
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; } } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。