在标签或文本框中显示 JSON 个值

Display JSON Values into labels or textbox

我正在尝试弄清楚如何将 JSON 文件直接显示到我的标签或文本框中,这就是我目前所得到的。

JSON 文件

"players": [
                {
                    "account_id": 85000810,
                    "name": "bb",
                    "hero_id": 0,
                    "team": 2
                },
                {
                    "account_id": 181829645,
                    "name": "Lobby: 0/0 Waifubot",
                    "hero_id": 0,
                    "team": 2
                },
                {
                    "account_id": 79119406,
                    "name": "Hitoshura",
                    "hero_id": 83,
                    "team": 1
                },
                {
                    "account_id": 48981143,
                    "name": "Mercury",
                    "hero_id": 93,
                    "team": 1
                },
                {
                    "account_id": 54661761,
                    "name": "Knockin' on Heaven's Door",
                    "hero_id": 9,
                    "team": 0
                },
                {
                    "account_id": 17124907,
                    "name": "xX_DoMiNaNt_DoMiNiC_Xx ◣◢)┌∩┐",
                    "hero_id": 57,
                    "team": 0
                },

我希望在我的 label1、label2、label3、label4 等中显示他们的名字..

这是我的代码:

LiveLeagues.LiveLeagues liveGames =JsonConvert.DeserializeObject<LiveLeagues.LiveLeagues>(response.Content.ReadAsStringAsync().Result);
foreach (var leagues in liveGames.Result.games)
{
   foreach (var players in leagues.players)
            {
               if (players.team == radiant_team)
                   {
                     AddRadiantPlayers(players.name, players.account_id, players.hero_id);
                   }
               if (players.team == dire_team)
                   {
                    AddDirePlayers(players.name, players.account_id, players.hero_id);
                   }
             }
}

目前我正在将他们的名字显示到 gridview 中,然后我将值传递到标签中,使用不可见的 gridview 作为桥梁是令人讨厌的。

你的json类似于下面的C#Type:

public class Player
{
    public int account_id { get; set; }
    public string name { get; set; }
    public int hero_id { get; set; }
    public int team { get; set; }
}

public class Container
{
    public List<Player> players { get; set; }
}

然后 var d = JsonConvert.DeserializeObject<Container>("yourjson").players; 你可以遍历 players 并做一些很棒的事情。