我想收集这个Json的信息:
{"name":"Maltarya","race":"Sylvari","gender":"Female","profession":"Thief","level":80,"equipment":[{"id":4483,"slot":"HelmAquatic","upgrades":[24723]},{"id":59,"slot":"Backpack","upgrades":[24498],"skin":2381},{"id":11805,"slot":"Coat","upgrades":[24815]},{"id":11889,"slot":"Boots","upgrades":[24723]},{"id":11847,"slot":"Gloves","upgrades":[24815]},{"id":11973,"slot":"Helm","upgrades":[24815]},{"id":11763,"slot":"Leggings","upgrades":[24815]},{"id":11931,"slot":"Shoulders","upgrades":[24815]},{"id":39141,"slot":"Accessory1","upgrades":[24545]}]}
但是当我想收集设备信息时,我有一个错误。我的代码是:
WebRequest request = WebRequest.Create("https://api.guildwars2.com/v2/characters/" + name + "?access_token=" + key);
var response = (HttpWebResponse)request.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
Personnages perso = JsonConvert.DeserializeObject<Personnages>(responseString);
以及我的Personnages课程:
class Personnages
{
public string name { get; set; }
public string race { get; set; }
public string gender { get; set; }
public string profession { get; set; }
public string level { get; set; }
public IList<string> equipment { get; set; }
}
我遇到的异常是:意外的令牌:读取字符串时出错。StartObject。
您正在尝试将JSON数组反序列化为IList<string>
。但是,此数组包含对象,但不包含字符串。
您需要为这些对象实现另一个类,并在反序列化中使用它:
class EquipmentItem
{
public int id { get; set; }
public string slot { get; set; }
public List<int> upgrades { get; set; }
}
class Personnages
{
public string name { get; set; }
public string race { get; set; }
public string gender { get; set; }
public string profession { get; set; }
public string level { get; set; }
public List<EquipmentItem> equipment { get; set; }
}
Personnages perso = JsonConvert.DeserializeObject<Personnages>(responseString);