【问题标题】:Display JSON Values into labels or textbox在标签或文本框中显示 JSON 值
【发布时间】:2015-06-07 05:03:22
【问题描述】:

我正在尝试弄清楚如何将 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 作为桥梁是很讨厌的。

【问题讨论】:

    标签: c# json winforms gridview


    【解决方案1】:

    你的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&lt;Container&gt;("yourjson").players; 你可以遍历 players 并做一些很棒的事情。

    【讨论】:

    • 是的,但我尝试将其映射到标签但失败了,我想知道它是否可以按索引映射,例如。标签1 = 播放器1,标签2 = 播放器2
    • 你可以像Label1.Text = players[0].name等一样
    • players[0].name 不起作用,因为每当我尝试放置索引时,属性 .name 都不会出现。
    • 这让你信服吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-28
    • 2014-11-14
    相关资源
    最近更新 更多