简体   繁体   中英

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. I used Json2CSharp in order to create the following data object.

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:

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. 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

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 :

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:

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. Note the name needs to be Data to match the name in the JSON.

The parser will automatically parse the JSON into the dictionary, using the strings as the dictionary key:

"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.

You can now easily access any property of each Champion easily. So your foreach loop can be:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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