簡體   English   中英

如何在 C# 中遍歷包含其他對象類型的對象

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

(這里完全是菜鳥,如果我不清楚,請見諒!)我一直在使用 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屬性來訪問鏈接到Dictionary Key部分的每個Champion對象

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM