将JSON值显示到标签或文本框

时间:2015-06-07 05:03:23

标签: c# json winforms gridview

我正在试图找出如何将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作为桥梁是令人讨厌的。

1 个答案:

答案 0 :(得分:0)

enter image description here

您的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并做出精彩的事情。

相关问题