[英]JSON error during parsing
一直在尋找有關如何解析此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的坦克添加“ Tank”標簽。 (同樣,請參見URL)
有人可以幫我嗎? 目前,我遇到了錯誤:
Newtonsoft.Json.dll中發生了類型為'Newtonsoft.Json.JsonSerializationException'的未處理異常。其他信息:無法將當前JSON對象(例如{“ name”:“ value”})反序列化為類型'System.Collections.Generic.List。 “ 1 [WotApp.Classes.Tank]”,因為該類型需要JSON數組(例如[1,2,3])才能正確反序列化。 要解決此錯誤,可以將JSON更改為JSON數組(例如[1,2,3]),也可以更改反序列化類型,使其成為普通的.NET類型(例如,不像整數這樣的原始類型,也不像這樣的集合類型。數組或列表),可以從JSON對象反序列化。 還可以將JsonObjectAttribute添加到類型中,以強制其從JSON對象反序列化。 路徑“ data.1”,第1行,位置39。
希望對您有所幫助。 我已經堅持了很長時間:(
還正在尋找答案,並且正在控制台應用程序測試中,@ Alexander和@dotctor的注釋實際上是正確的答案;)因此,您的類必須像這樣:
public class Response
{
public string status { get; set; }
public int count { get; set; }
public Dictionary<String, Tank> data { get; set; }
}
而我的程序清單:
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.