C# 从传说联盟API反序列化JSON时出错
我正在尝试使用RiotgamesAPI。我有JSON数据,需要将此JSON反序列化为c#类,但我得到一个错误: Newtonsoft.Json.JsonSerializationException:'无法将当前Json对象(例如{“名称”:“值”})反序列化为类型'System.Collections.Generic.List`1[WFAleAgueOffleGendSapi.Heroes],因为该类型需要Json数组(例如[1,2,3])才能正确反序列化。 若要修复此错误,请将JSON更改为JSON数组(例如[1,2,3]),或更改反序列化类型,使其成为可以从JSON对象反序列化的正常.NET类型(例如,不是integer之类的基元类型,也不是数组或列表之类的集合类型)。还可以将JsonObjectAttribute添加到类型中,以强制它从JSON对象反序列化。 路径'datas.Aatrox',第1行,位置85' 我的班级:C# 从传说联盟API反序列化JSON时出错,c#,json,json.net,C#,Json,Json.net,我正在尝试使用RiotgamesAPI。我有JSON数据,需要将此JSON反序列化为c#类,但我得到一个错误: Newtonsoft.Json.JsonSerializationException:'无法将当前Json对象(例如{“名称”:“值”})反序列化为类型'System.Collections.Generic.List`1[WFAleAgueOffleGendSapi.Heroes],因为该类型需要Json数组(例如[1,2,3])才能正确反序列化。 若要修复此错误,请将JSON更改为J
public class JsonRoot
{
public string type { get; set; }
public string format { get; set; }
public string version { get; set; }
public List<Heroes> datas { get; set; }
}
public class Heroes
{
public HeroesData Name { get; set; }
}
public class HeroesData
{
public string version { get; set; }
public string id { get; set; }
public string key { get; set; }
public string name { get; set; }
public string title { get; set; }
public HeroImage image { get; set; }
}
public class HeroImage
{
public string full { get; set; }
public string sprite { get; set; }
public string group { get; set; }
public override string ToString()
{
return full;
}
}
公共类JsonRoot
{
公共字符串类型{get;set;}
公共字符串格式{get;set;}
公共字符串版本{get;set;}
公共列表数据{get;set;}
}
公众阶级英雄
{
公共HeroesData名称{get;set;}
}
公共类HeroesData
{
公共字符串版本{get;set;}
公共字符串id{get;set;}
公共字符串密钥{get;set;}
公共字符串名称{get;set;}
公共字符串标题{get;set;}
公共图像{get;set;}
}
公共级英雄法师
{
公共字符串已满{get;set;}
公共字符串sprite{get;set;}
公共字符串组{get;set;}
公共重写字符串ToString()
{
全额退还;
}
}
C#代码:
var json=new WebClient()。下载字符串(“http://ddragon.leagueoflegends.com/cdn/6.24.1/data/en_US/champion.json");
json=json.Replace(“数据”、“数据”);
JsonRoot jr=JsonConvert.DeserializeObject(json);//这行有错误
之所以出现此错误,是因为您对数据使用了列表
,但该属性不是JSON中的数组。您需要改用词典
。英雄的名字将成为字典的关键。此外,如果您想为类中的特定属性使用与JSON中不同的名称,可以使用[JsonProperty]
属性,如下所示。使用string.Replace
来尝试更改JSON以适合您的类,这并不是一个好主意,因为您可能最终会替换您不想要的东西
public class JsonRoot
{
public string type { get; set; }
public string format { get; set; }
public string version { get; set; }
[JsonProperty("data")]
public Dictionary<string, HeroesData> heroes { get; set; }
}
公共类JsonRoot
{
公共字符串类型{get;set;}
公共字符串格式{get;set;}
公共字符串版本{get;set;}
[JsonProperty(“数据”)]
公共字典英雄{get;set;}
}
Fiddle:您之所以会出现此错误,是因为您对数据使用了列表,但该属性不是JSON中的数组。您需要改用词典
。英雄的名字将成为字典的关键。此外,如果您想为类中的特定属性使用与JSON中不同的名称,可以使用[JsonProperty]
属性,如下所示。使用string.Replace
来尝试更改JSON以适合您的类,这并不是一个好主意,因为您可能最终会替换您不想要的东西
public class JsonRoot
{
public string type { get; set; }
public string format { get; set; }
public string version { get; set; }
[JsonProperty("data")]
public Dictionary<string, HeroesData> heroes { get; set; }
}
公共类JsonRoot
{
公共字符串类型{get;set;}
公共字符串格式{get;set;}
公共字符串版本{get;set;}
[JsonProperty(“数据”)]
公共字典英雄{get;set;}
}
Fiddle:通常Newtsoft会很好地处理数组,但作为一个简单的黑客,只需将Heros list更改为数组,该属性名为data
而不是datas
。重命名属性或使用JsonPropertyAttribute
:[JsonProperty(PropertyName=“data”)]
。切勿使用Replace
重新格式化数据。@SaniSinghHuttunen我尝试了您的建议,但还是出现了相同的错误。如果使用replace for data error fixed但返回null:/json数据“格式不正确”data
是一个json对象
,而不是一个json对象数组
。您正在尝试将json对象反序列化为json数组<代码>数据
由几个属性组成,每个属性都以用户id命名。例如,Aatrox
,Ahri
,等等。您需要为每个属性创建一个新类或使用自定义解析。@sanisinghhuttune我现在明白了。我不需要类型、格式和版本,我只需要数据。我如何获得它?通常Newtsoft会很好地处理数组列表,但作为一个简单的黑客,只需将Heros list更改为一个数组,该属性名为data
而不是datas
。重命名属性或使用JsonPropertyAttribute
:[JsonProperty(PropertyName=“data”)]
。切勿使用Replace
重新格式化数据。@SaniSinghHuttunen我尝试了您的建议,但还是出现了相同的错误。如果使用replace for data error fixed但返回null:/json数据“格式不正确”data
是一个json对象
,而不是一个json对象数组
。您正在尝试将json对象反序列化为json数组<代码>数据
由几个属性组成,每个属性都以用户id命名。例如,Aatrox
,Ahri
,等等。您需要为每个属性创建一个新类或使用自定义解析。@sanisinghhuttune我现在明白了。我不需要类型、格式和版本,我只需要数据。我怎么得到它?