简体   繁体   中英

Display JSON Values into labels or textbox

I'm trying to figure out how to display JSON file directly into my labels or textboxes, this what i got so far.

JSON File

"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
                },

i wish to display their names into my label1, label2, label3, label4 and so on..

this is my code:

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);
                   }
             }
}

currently i'm displaying their names into the gridview then from that i pass the values into the labels, it's nasty to use invisible gridview as a bridge.

在此处输入图片说明

your json is similar to the following 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; }
}

then var d = JsonConvert.DeserializeObject<Container>("yourjson").players; you can iterate over players and do wonderful things.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM