((如果不清楚,请在此处完成菜鸟操作,如果不知道,请提供应用!),我一直在使用Riot Games的API来获取有关游戏中特定角色的玩家统计信息。我使用Json2CSharp来创建以下数据对象。
public class Data
{
public Aatrox Aatrox { get; set; }
public Ahri Ahri { get; set; }
public Akali Akali { get; set; }
public Alistar Alistar { get; set; }
public Amumu Amumu { get; set; }
public Anivia Anivia { get; set; }
public Annie Annie { get; set; }
public Ashe Ashe { get; set; }
public AurelionSol AurelionSol { get; set; }
public Azir Azir { get; set; }
public Bard Bard { get; set; }
public Blitzcrank Blitzcrank { get; set; }
public Brand Brand { get; set; }
public Braum Braum { get; set; }
public Caitlyn Caitlyn { get; set; }
public Camille Camille { get; set; }
public Cassiopeia Cassiopeia { get; set; }
public Chogath Chogath { get; set; }
public Corki Corki { get; set; }
public Darius Darius { get; set; }
public Diana Diana { get; set; }
public Draven Draven { get; set; }
public DrMundo DrMundo { get; set; }
public Ekko Ekko { get; set; }
public Elise Elise { get; set; }
public Evelynn Evelynn { get; set; }
public Ezreal Ezreal { get; set; }
public FiddleSticks FiddleSticks { get; set; }
public Fiora Fiora { get; set; }
public Fizz Fizz { get; set; }
public Galio Galio { get; set; }
public Gangplank Gangplank { get; set; }
public Garen Garen { get; set; }
public Gnar Gnar { get; set; }
public Gragas Gragas { get; set; }
public Graves Graves { get; set; }
public Hecarim Hecarim { get; set; }
public Heimerdinger Heimerdinger { get; set; }
public Illaoi Illaoi { get; set; }
public Irelia Irelia { get; set; }
public Ivern Ivern { get; set; }
public Janna Janna { get; set; }
public JarvanIV JarvanIV { get; set; }
public Jax Jax { get; set; }
public Jayce Jayce { get; set; }
public Jhin Jhin { get; set; }
public Jinx Jinx { get; set; }
public Kalista Kalista { get; set; }
public Karma Karma { get; set; }
public Karthus Karthus { get; set; }
public Kassadin Kassadin { get; set; }
public Katarina Katarina { get; set; }
public Kayle Kayle { get; set; }
public Kennen Kennen { get; set; }
public Khazix Khazix { get; set; }
public Kindred Kindred { get; set; }
public Kled Kled { get; set; }
public KogMaw KogMaw { get; set; }
public Leblanc Leblanc { get; set; }
public LeeSin LeeSin { get; set; }
public Leona Leona { get; set; }
public Lissandra Lissandra { get; set; }
public Lucian Lucian { get; set; }
public Lulu Lulu { get; set; }
public Lux Lux { get; set; }
public Malphite Malphite { get; set; }
public Malzahar Malzahar { get; set; }
public Maokai Maokai { get; set; }
public MasterYi MasterYi { get; set; }
public MissFortune MissFortune { get; set; }
public MonkeyKing MonkeyKing { get; set; }
public Mordekaiser Mordekaiser { get; set; }
public Morgana Morgana { get; set; }
public Nami Nami { get; set; }
public Nasus Nasus { get; set; }
public Nautilus Nautilus { get; set; }
public Nidalee Nidalee { get; set; }
public Nocturne Nocturne { get; set; }
public Nunu Nunu { get; set; }
public Olaf Olaf { get; set; }
public Orianna Orianna { get; set; }
public Pantheon Pantheon { get; set; }
public Poppy Poppy { get; set; }
public Quinn Quinn { get; set; }
public Rammus Rammus { get; set; }
public RekSai RekSai { get; set; }
public Renekton Renekton { get; set; }
public Rengar Rengar { get; set; }
public Riven Riven { get; set; }
public Rumble Rumble { get; set; }
public Ryze Ryze { get; set; }
public Sejuani Sejuani { get; set; }
public Shaco Shaco { get; set; }
public Shen Shen { get; set; }
public Shyvana Shyvana { get; set; }
public Singed Singed { get; set; }
public Sion Sion { get; set; }
public Sivir Sivir { get; set; }
public Skarner Skarner { get; set; }
public Sona Sona { get; set; }
public Soraka Soraka { get; set; }
public Swain Swain { get; set; }
public Syndra Syndra { get; set; }
public TahmKench TahmKench { get; set; }
public Taliyah Taliyah { get; set; }
public Talon Talon { get; set; }
public Taric Taric { get; set; }
public Teemo Teemo { get; set; }
public Thresh Thresh { get; set; }
public Tristana Tristana { get; set; }
public Trundle Trundle { get; set; }
public Tryndamere Tryndamere { get; set; }
public TwistedFate TwistedFate { get; set; }
public Twitch Twitch { get; set; }
public Udyr Udyr { get; set; }
public Urgot Urgot { get; set; }
public Varus Varus { get; set; }
public Vayne Vayne { get; set; }
public Veigar Veigar { get; set; }
public Velkoz Velkoz { get; set; }
public Vi Vi { get; set; }
public Viktor Viktor { get; set; }
public Vladimir Vladimir { get; set; }
public Volibear Volibear { get; set; }
public Warwick Warwick { get; set; }
public Xerath Xerath { get; set; }
public XinZhao XinZhao { get; set; }
public Yasuo Yasuo { get; set; }
public Yorick Yorick { get; set; }
public Zac Zac { get; set; }
public Zed Zed { get; set; }
public Ziggs Ziggs { get; set; }
public Zilean Zilean { get; set; }
public Zyra Zyra { get; set; }
}
每个冠军的各自对象看起来像下面:
public class Ziggs
{
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 string blurb { get; set; }
public Info132 info { get; set; }
public Image132 image { get; set; }
public List<string> tags { get; set; }
public string partype { get; set; }
public Stats132 stats { get; set; }
}
我想做的是获得每个冠军的ID,并且一直在尝试下面的for循环:
foreach (var champion in ChampionData.data.GetType().GetProperties())
{
Console.WriteLine(champion.id);
}
但是,这似乎不允许我访问ID,也不允许访问对象的任何其他属性。我已经能够访问在视觉上调用它们的值,但是无法弄清楚如何遍历该对象。
任何建议都将不胜感激!
编辑:这是我从以下位置获得Json的地方:http://ddragon.leagueoflegends.com/cdn/6.24.1/data/en_US/champion.json
如评论中所述,您需要定义一个代表所有冠军的单一班级。在下面的示例中,我只是将您问题中的Ziggs
类重命名为Champion
:
public class Champion
{
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 string blurb { get; set; }
public Info132 info { get; set; }
public Image132 image { get; set; }
public List<string> tags { get; set; }
public string partype { get; set; }
public Stats132 stats { get; set; }
}
现在您需要更改ChampionData
类以使用字典存储这些对象:
public class ChampionData
{
public string Type { get; set; }
public string Format { get; set; }
public string Version { get; set; }
public Dictionary<string, Champion> Data { get; set; }
}
我们使用Dictionary<string, Champion>
表示JSON中的data
元素。请注意,名称必须为Data
以匹配JSON中的名称。
解析器将使用字符串作为字典键,将JSON自动解析为字典:
"data": {
"Aatrox": { "..." },
"Ahri": { "..." },
"Akali": { "..." },
因此,“ Aatrox”,“ Ahri”,“ Akali”将成为字典的string
部分(即Key
),而字典的Champion
部分(即Value
)将包含JSON中每个冠军名下的所有数据。
您现在可以轻松地轻松访问每个Champion
的任何属性。因此,您的foreach
循环可以是:
foreach (var c in data.Data)
{
Console.WriteLine(c.Value.id);
}
注意:我们需要使用Value
属性访问链接到Champion
的Key
部分的每个Dictionary
对象>