2015-12-02 112 views
2

我要收集此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。

回答

1

您试图反序列化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);