代码之家  ›  专栏  ›  技术社区  ›  Lars

从Json数组中获取项

  •  0
  • Lars  · 技术社区  · 2 年前

    我想从JSON数组中获取多个项的字符串/整数,但我真的不知道如何实现这一点

    static void Main(string[] args)
            {
                Console.WriteLine(jsonRequest("Râsu", "Aegwynn", "mythic_plus_best_runs").SelectToken("mythic_plus_best_runs"));
                Console.ReadKey();
            }
    
            public static dynamic jsonRequest(String playerName, String realmName, String requestField)
            {
                String json = new WebClient().DownloadString("https://raider.io/api/v1/characters/profile?region=eu&realm=" + realmName + "&name=" + playerName + "&fields=" + requestField);
                dynamic liste = JsonConvert.DeserializeObject(json);
                return liste;
            }
    
    using Newtonsoft.Json;
    using System;
    using System.Net;
    

    在这个项目中得到了应用

    我想为每一件物品提取“地下城”和“钥匙级别”字符串/int,但当我这么做时

    Console.WriteLine(jsonRequest("Râsu", "Aegwynn", "mythic_plus_best_runs").SelectToken("mythic_plus_best_runs.dungeon"));
    

    它只是扔

        {
            "name": "Râsu",
            "race": "Worgen",
            "class": "Druid",
            "active_spec_name": "Balance",
            "active_spec_role": "DPS",
            "gender": "male",
            "faction": "alliance",
            "achievement_points": 15570,
            "honorable_kills": 0,
            "thumbnail_url": "https://render-eu.worldofwarcraft.com/character/aegwynn/240/202932208-avatar.jpg?alt=wow/static/images/2d/avatar/22-0.jpg",
            "region": "eu",
            "realm": "Aegwynn",
            "last_crawled_at": "2022-03-22T08:24:35.000Z",
            "profile_url": "https://raider.io/characters/eu/aegwynn/Râsu",
            "profile_banner": "alliancebanner1",
            "mythic_plus_best_runs": [
              {
                "dungeon": "De Other Side",
                "short_name": "DOS",
                "mythic_level": 18,
                "completed_at": "2022-03-17T18:19:03.000Z",
                "clear_time_ms": 2274234,
                "par_time_ms": 2580999,
                "num_keystone_upgrades": 1,
                "map_challenge_mode_id": 377,
                "zone_id": 13309,
                "score": 141.5,
                "affixes": [
                  {
                    "id": 9,
                    "name": "Tyrannical",
                    "description": "Bosses have 30% more health. Bosses and their minions inflict up to 15% increased damage.",
                    "icon": "achievement_boss_archaedas",
                    "wowhead_url": "https://wowhead.com/affix=9"
                  },
                  {
                    "id": 6,
                    "name": "Raging",
                    "description": "Non-boss enemies enrage at 30% health remaining, dealing 50% increased damage until defeated.",
                    "icon": "ability_warrior_focusedrage",
                    "wowhead_url": "https://wowhead.com/affix=6"
                  },
                  {
                    "id": 3,
                    "name": "Volcanic",
                    "description": "While in combat, enemies periodically cause gouts of flame to erupt beneath the feet of distant players.",
                    "icon": "spell_shaman_lavasurge",
                    "wowhead_url": "https://wowhead.com/affix=3"
                  },
                  {
                    "id": 130,
                    "name": "Encrypted",
                    "description": "Enemies throughout the dungeon possess relics of the First Ones. Destroy the relics to summon the First Ones' Automa and gain powerful bonuses, based on the order in which they were destroyed.",
                    "icon": "spell_progenitor_orb",
                    "wowhead_url": "https://wowhead.com/affix=130"
                  }
                ],
                "url": "https://raider.io/mythic-plus-runs/season-sl-3/2154109-18-de-other-side"
              },
              {
                "dungeon": "Spires of Ascension",
                "short_name": "SOA",
                "mythic_level": 18,
                "completed_at": "2022-03-19T19:23:45.000Z",
                "clear_time_ms": 2483104,
                "par_time_ms": 2340999,
                "num_keystone_upgrades": 0,
                "map_challenge_mode_id": 381,
                "zone_id": 12837,
                "score": 134.2,
                "affixes": [
                  {
                    "id": 9,
                    "name": "Tyrannical",
                    "description": "Bosses have 30% more health. Bosses and their minions inflict up to 15% increased damage.",
                    "icon": "achievement_boss_archaedas",
                    "wowhead_url": "https://wowhead.com/affix=9"
                  },
                  {
                    "id": 6,
                    "name": "Raging",
                    "description": "Non-boss enemies enrage at 30% health remaining, dealing 50% increased damage until defeated.",
                    "icon": "ability_warrior_focusedrage",
                    "wowhead_url": "https://wowhead.com/affix=6"
                  },
                  {
                    "id": 3,
                    "name": "Volcanic",
                    "description": "While in combat, enemies periodically cause gouts of flame to erupt beneath the feet of distant players.",
                    "icon": "spell_shaman_lavasurge",
                    "wowhead_url": "https://wowhead.com/affix=3"
                  },
                  {
                    "id": 130,
                    "name": "Encrypted",
                    "description": "Enemies throughout the dungeon possess relics of the First Ones. Destroy the relics to summon the First Ones' Automa and gain powerful bonuses, based on the order in which they were destroyed.",
                    "icon": "spell_progenitor_orb",
                    "wowhead_url": "https://wowhead.com/affix=130"
                  }
                ],
                "url": "https://raider.io/mythic-plus-runs/season-sl-3/2540683-18-spires-of-ascension"
              },
              {
                "dungeon": "Plaguefall",
                "short_name": "PF",
                "mythic_level": 16,
                "completed_at": "2022-03-17T17:31:37.000Z",
                "clear_time_ms": 1641342,
                "par_time_ms": 2280999,
                "num_keystone_upgrades": 2,
                "map_challenge_mode_id": 379,
                "zone_id": 13228,
                "score": 133.5,
                "affixes": [
                  {
                    "id": 9,
                    "name": "Tyrannical",
                    "description": "Bosses have 30% more health. Bosses and their minions inflict up to 15% increased damage.",
                    "icon": "achievement_boss_archaedas",
                    "wowhead_url": "https://wowhead.com/affix=9"
                  },
                  {
                    "id": 6,
                    "name": "Raging",
                    "description": "Non-boss enemies enrage at 30% health remaining, dealing 50% increased damage until defeated.",
                    "icon": "ability_warrior_focusedrage",
                    "wowhead_url": "https://wowhead.com/affix=6"
                  },
                  {
                    "id": 3,
                    "name": "Volcanic",
                    "description": "While in combat, enemies periodically cause gouts of flame to erupt beneath the feet of distant players.",
                    "icon": "spell_shaman_lavasurge",
                    "wowhead_url": "https://wowhead.com/affix=3"
                  },
                  {
                    "id": 130,
                    "name": "Encrypted",
                    "description": "Enemies throughout the dungeon possess relics of the First Ones. Destroy the relics to summon the First Ones' Automa and gain powerful bonuses, based on the order in which they were destroyed.",
                    "icon": "spell_progenitor_orb",
                    "wowhead_url": "https://wowhead.com/affix=130"
                  }
                ],
                "url": "https://raider.io/mythic-plus-runs/season-sl-3/2149124-16-plaguefall"
              },
              {
                "dungeon": "Theater of Pain",
                "short_name": "TOP",
                "mythic_level": 16,
                "completed_at": "2022-03-19T21:57:57.000Z",
                "clear_time_ms": 2127992,
                "par_time_ms": 2280999,
                "num_keystone_upgrades": 1,
                "map_challenge_mode_id": 382,
                "zone_id": 12841,
                "score": 130.8,
                "affixes": [
                  {
                    "id": 9,
                    "name": "Tyrannical",
                    "description": "Bosses have 30% more health. Bosses and their minions inflict up to 15% increased damage.",
                    "icon": "achievement_boss_archaedas",
                    "wowhead_url": "https://wowhead.com/affix=9"
                  },
                  {
                    "id": 6,
                    "name": "Raging",
                    "description": "Non-boss enemies enrage at 30% health remaining, dealing 50% increased damage until defeated.",
                    "icon": "ability_warrior_focusedrage",
                    "wowhead_url": "https://wowhead.com/affix=6"
                  },
                  {
                    "id": 3,
                    "name": "Volcanic",
                    "description": "While in combat, enemies periodically cause gouts of flame to erupt beneath the feet of distant players.",
                    "icon": "spell_shaman_lavasurge",
                    "wowhead_url": "https://wowhead.com/affix=3"
                  },
                  {
                    "id": 130,
                    "name": "Encrypted",
                    "description": "Enemies throughout the dungeon possess relics of the First Ones. Destroy the relics to summon the First Ones' Automa and gain powerful bonuses, based on the order in which they were destroyed.",
                    "icon": "spell_progenitor_orb",
                    "wowhead_url": "https://wowhead.com/affix=130"
                  }
                ],
                "url": "https://raider.io/mythic-plus-runs/season-sl-3/2544779-16-theater-of-pain"
              },
              {
                "dungeon": "Halls of Atonement",
                "short_name": "HOA",
                "mythic_level": 15,
                "completed_at": "2022-03-19T20:28:03.000Z",
                "clear_time_ms": 1359135,
                "par_time_ms": 1920999,
                "num_keystone_upgrades": 2,
                "map_challenge_mode_id": 378,
                "zone_id": 12831,
                "score": 128.7,
                "affixes": [
                  {
                    "id": 9,
                    "name": "Tyrannical",
                    "description": "Bosses have 30% more health. Bosses and their minions inflict up to 15% increased damage.",
                    "icon": "achievement_boss_archaedas",
                    "wowhead_url": "https://wowhead.com/affix=9"
                  },
                  {
                    "id": 6,
                    "name": "Raging",
                    "description": "Non-boss enemies enrage at 30% health remaining, dealing 50% increased damage until defeated.",
                    "icon": "ability_warrior_focusedrage",
                    "wowhead_url": "https://wowhead.com/affix=6"
                  },
                  {
                    "id": 3,
                    "name": "Volcanic",
                    "description": "While in combat, enemies periodically cause gouts of flame to erupt beneath the feet of distant players.",
                    "icon": "spell_shaman_lavasurge",
                    "wowhead_url": "https://wowhead.com/affix=3"
                  },
                  {
                    "id": 130,
                    "name": "Encrypted",
                    "description": "Enemies throughout the dungeon possess relics of the First Ones. Destroy the relics to summon the First Ones' Automa and gain powerful bonuses, based on the order in which they were destroyed.",
                    "icon": "spell_progenitor_orb",
                    "wowhead_url": "https://wowhead.com/affix=130"
                  }
                ],
                "url": "https://raider.io/mythic-plus-runs/season-sl-3/2544778-15-halls-of-atonement"
              },
              {
                "dungeon": "Sanguine Depths",
                "short_name": "SD",
                "mythic_level": 15,
                "completed_at": "2022-03-19T21:13:44.000Z",
                "clear_time_ms": 1827333,
                "par_time_ms": 2460999,
                "num_keystone_upgrades": 2,
                "map_challenge_mode_id": 380,
                "zone_id": 12842,
                "score": 128.2,
                "affixes": [
                  {
                    "id": 9,
                    "name": "Tyrannical",
                    "description": "Bosses have 30% more health. Bosses and their minions inflict up to 15% increased damage.",
                    "icon": "achievement_boss_archaedas",
                    "wowhead_url": "https://wowhead.com/affix=9"
                  },
                  {
                    "id": 6,
                    "name": "Raging",
                    "description": "Non-boss enemies enrage at 30% health remaining, dealing 50% increased damage until defeated.",
                    "icon": "ability_warrior_focusedrage",
                    "wowhead_url": "https://wowhead.com/affix=6"
                  },
                  {
                    "id": 3,
                    "name": "Volcanic",
                    "description": "While in combat, enemies periodically cause gouts of flame to erupt beneath the feet of distant players.",
                    "icon": "spell_shaman_lavasurge",
                    "wowhead_url": "https://wowhead.com/affix=3"
                  },
                  {
                    "id": 130,
                    "name": "Encrypted",
                    "description": "Enemies throughout the dungeon possess relics of the First Ones. Destroy the relics to summon the First Ones' Automa and gain powerful bonuses, based on the order in which they were destroyed.",
                    "icon": "spell_progenitor_orb",
                    "wowhead_url": "https://wowhead.com/affix=130"
                  }
                ],
                "url": "https://raider.io/mythic-plus-runs/season-sl-3/2544767-15-sanguine-depths"
              },
              {
                "dungeon": "The Necrotic Wake",
                "short_name": "NW",
                "mythic_level": 15,
                "completed_at": "2022-03-17T16:31:27.000Z",
                "clear_time_ms": 1649375,
                "par_time_ms": 2160999,
                "num_keystone_upgrades": 2,
                "map_challenge_mode_id": 376,
                "zone_id": 12916,
                "score": 128,
                "affixes": [
                  {
                    "id": 9,
                    "name": "Tyrannical",
                    "description": "Bosses have 30% more health. Bosses and their minions inflict up to 15% increased damage.",
                    "icon": "achievement_boss_archaedas",
                    "wowhead_url": "https://wowhead.com/affix=9"
                  },
                  {
                    "id": 6,
                    "name": "Raging",
                    "description": "Non-boss enemies enrage at 30% health remaining, dealing 50% increased damage until defeated.",
                    "icon": "ability_warrior_focusedrage",
                    "wowhead_url": "https://wowhead.com/affix=6"
                  },
                  {
                    "id": 3,
                    "name": "Volcanic",
                    "description": "While in combat, enemies periodically cause gouts of flame to erupt beneath the feet of distant players.",
                    "icon": "spell_shaman_lavasurge",
                    "wowhead_url": "https://wowhead.com/affix=3"
                  },
                  {
                    "id": 130,
                    "name": "Encrypted",
                    "description": "Enemies throughout the dungeon possess relics of the First Ones. Destroy the relics to summon the First Ones' Automa and gain powerful bonuses, based on the order in which they were destroyed.",
                    "icon": "spell_progenitor_orb",
                    "wowhead_url": "https://wowhead.com/affix=130"
                  }
                ],
                "url": "https://raider.io/mythic-plus-runs/season-sl-3/2142513-15-the-necrotic-wake"
              },
              {
                "dungeon": "Mists of Tirna Scithe",
                "short_name": "MISTS",
                "mythic_level": 17,
                "completed_at": "2022-03-09T15:25:20.000Z",
                "clear_time_ms": 2198966,
                "par_time_ms": 1800999,
                "num_keystone_upgrades": 0,
                "map_challenge_mode_id": 375,
                "zone_id": 13334,
                "score": 127.2,
                "affixes": [
                  {
                    "id": 10,
                    "name": "Fortified",
                    "description": "Non-boss enemies have 20% more health and inflict up to 30% increased damage.",
                    "icon": "ability_toughness",
                    "wowhead_url": "https://wowhead.com/affix=10"
                  },
                  {
                    "id": 11,
                    "name": "Bursting",
                    "description": "When slain, non-boss enemies explode, causing all players to suffer damage over 4 sec. This effect stacks.",
                    "icon": "ability_ironmaidens_whirlofblood",
                    "wowhead_url": "https://wowhead.com/affix=11"
                  },
                  {
                    "id": 124,
                    "name": "Storming",
                    "description": "While in combat, enemies periodically summon damaging whirlwinds.",
                    "icon": "spell_nature_cyclone",
                    "wowhead_url": "https://wowhead.com/affix=124"
                  },
                  {
                    "id": 130,
                    "name": "Encrypted",
                    "description": "Enemies throughout the dungeon possess relics of the First Ones. Destroy the relics to summon the First Ones' Automa and gain powerful bonuses, based on the order in which they were destroyed.",
                    "icon": "spell_progenitor_orb",
                    "wowhead_url": "https://wowhead.com/affix=130"
                  }
                ],
                "url": "https://raider.io/mythic-plus-runs/season-sl-3/956881-17-mists-of-tirna-scithe"
              },
              {
                "dungeon": "Tazavesh: So'leah's Gambit",
                "short_name": "GMBT",
                "mythic_level": 15,
                "completed_at": "2022-03-20T18:00:05.000Z",
                "clear_time_ms": 1529610,
                "par_time_ms": 1800999,
                "num_keystone_upgrades": 1,
                "map_challenge_mode_id": 392,
                "zone_id": 1000001,
                "score": 126.9,
                "affixes": [
                  {
                    "id": 9,
                    "name": "Tyrannical",
                    "description": "Bosses have 30% more health. Bosses and their minions inflict up to 15% increased damage.",
                    "icon": "achievement_boss_archaedas",
                    "wowhead_url": "https://wowhead.com/affix=9"
                  },
                  {
                    "id": 6,
                    "name": "Raging",
                    "description": "Non-boss enemies enrage at 30% health remaining, dealing 50% increased damage until defeated.",
                    "icon": "ability_warrior_focusedrage",
                    "wowhead_url": "https://wowhead.com/affix=6"
                  },
                  {
                    "id": 3,
                    "name": "Volcanic",
                    "description": "While in combat, enemies periodically cause gouts of flame to erupt beneath the feet of distant players.",
                    "icon": "spell_shaman_lavasurge",
                    "wowhead_url": "https://wowhead.com/affix=3"
                  },
                  {
                    "id": 130,
                    "name": "Encrypted",
                    "description": "Enemies throughout the dungeon possess relics of the First Ones. Destroy the relics to summon the First Ones' Automa and gain powerful bonuses, based on the order in which they were destroyed.",
                    "icon": "spell_progenitor_orb",
                    "wowhead_url": "https://wowhead.com/affix=130"
                  }
                ],
                "url": "https://raider.io/mythic-plus-runs/season-sl-3/2626552-15-tazavesh-soleahs-gambit"
              },
              {
                "dungeon": "Tazavesh: Streets of Wonder",
                "short_name": "STRT",
                "mythic_level": 14,
                "completed_at": "2022-03-20T16:46:08.000Z",
                "clear_time_ms": 1887336,
                "par_time_ms": 2340999,
                "num_keystone_upgrades": 1,
                "map_challenge_mode_id": 391,
                "zone_id": 1000000,
                "score": 122.4,
                "affixes": [
                  {
                    "id": 9,
                    "name": "Tyrannical",
                    "description": "Bosses have 30% more health. Bosses and their minions inflict up to 15% increased damage.",
                    "icon": "achievement_boss_archaedas",
                    "wowhead_url": "https://wowhead.com/affix=9"
                  },
                  {
                    "id": 6,
                    "name": "Raging",
                    "description": "Non-boss enemies enrage at 30% health remaining, dealing 50% increased damage until defeated.",
                    "icon": "ability_warrior_focusedrage",
                    "wowhead_url": "https://wowhead.com/affix=6"
                  },
                  {
                    "id": 3,
                    "name": "Volcanic",
                    "description": "While in combat, enemies periodically cause gouts of flame to erupt beneath the feet of distant players.",
                    "icon": "spell_shaman_lavasurge",
                    "wowhead_url": "https://wowhead.com/affix=3"
                  },
                  {
                    "id": 130,
                    "name": "Encrypted",
                    "description": "Enemies throughout the dungeon possess relics of the First Ones. Destroy the relics to summon the First Ones' Automa and gain powerful bonuses, based on the order in which they were destroyed.",
                    "icon": "spell_progenitor_orb",
                    "wowhead_url": "https://wowhead.com/affix=130"
                  }
                ],
                "url": "https://raider.io/mythic-plus-runs/season-sl-3/2650220-14-tazavesh-streets-of-wonder"
              }
            ]
          }

    这就是我需要的Json文件中的值

    1 回复  |  直到 2 年前
        1
  •  0
  •   Paulo Pereira    2 年前

    密钥“mythic_plus_best_runs”的值是一个数组。

    所以,你必须循环它来获得所有的“地牢”值。

    var result = jsonRequest("Râsu", "Aegwynn", "mythic_plus_best_runs");
    
    foreach (var item in result["mythic_plus_best_runs"])
    {
        Console.WriteLine(item["dungeon"]);
    }