简体   繁体   English

如何在 C# 中遍历包含其他对象类型的对象

[英]How to iterate through object containing other object types in C#

(Complete noob here, appologies if I'm unclear!) I have been using Riot Games' API to get information of a players stats on specific characters in a game. (这里完全是菜鸟,如果我不清楚,请见谅!)我一直在使用 Riot Games 的 API 来获取游戏中特定角色的玩家统计信息。 I used Json2CSharp in order to create the following data object.我使用 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; }

        }

each champion's respective object looking like the below:每个冠军各自的对象如下所示:

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; }
        }

What I'm trying to do is get at the ID for each champion, and have been trying with the for loop below:我想要做的是获取每个冠军的 ID,并且一直在尝试使用下面的 for 循环:

foreach (var champion in ChampionData.data.GetType().GetProperties())
            {
                Console.WriteLine(champion.id);
            }

This however does not seem to allow me to access the ID, nor any of the other properties of the objects.然而,这似乎不允许我访问 ID,也不允许我访问对象的任何其他属性。 I have been able to access the values calling them indervisually, however cannot figure out how I can iterate through the object.我已经能够以无形的方式访问调用它们的值,但是无法弄清楚如何遍历对象。

Any advise would be really appreciated!任何建议将不胜感激!

Edit: Here is where I'm getting the Json from: http://ddragon.leagueoflegends.com/cdn/6.24.1/data/en_US/champion.json编辑:这是我从这里获取 Json 的地方: http : //ddragon.leagueoflegends.com/cdn/6.24.1/data/en_US/champion.json

As described in the comments you need to define a single class that represents all of the Champions.如评论中所述,您需要定义一个代表所有冠军的类。 In the example below I have simply renamed the Ziggs class in your question to Champion :在下面的示例中,我只是将您问题中的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; }
}

Now you need to change your ChampionData class to use a dictionary to store these objects:现在您需要更改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; }
}

We have used a Dictionary<string, Champion> to represent the data elements in the JSON.我们使用Dictionary<string, Champion>来表示 JSON 中的data元素。 Note the name needs to be Data to match the name in the JSON.请注意,名称必须是Data才能匹配 JSON 中的名称。

The parser will automatically parse the JSON into the dictionary, using the strings as the dictionary key:解析器会自动将 JSON 解析到字典中,使用字符串作为字典键:

"data": {
    "Aatrox": { "..." },
    "Ahri": { "..." },
    "Akali": { "..." },

So, "Aatrox", "Ahri", "Akali" will be the string part of the dictionary (ie the Key ) and the Champion part of the dictionary (ie the Value ) will contain all the data that is under each champion in the JSON.因此,“Aatrox”、“Ahri”、“Akali”将是字典的string部分(即Key ),而字典的Champion部分(即Value )将包含每个英雄下的所有数据JSON。

You can now easily access any property of each Champion easily.您现在可以轻松访问每个Champion任何属性。 So your foreach loop can be:所以你的foreach循环可以是:

foreach (var c in data.Data)
{
    Console.WriteLine(c.Value.id);
}

NOTE: We need to use the Value property to access each Champion object that is linked to the Key part of the Dictionary注意:我们需要使用Value属性来访问链接到Dictionary Key部分的每个Champion对象

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM