从json文件中读取数据并设置为组合框项目

时间:2014-11-03 14:47:17

标签: c# winforms json.net

我在下面有这个json文件。我已经下载了它。我想用这个json文件中的数据填充一个组合框。如何在win表单应用程序(c#)中执行此操作?

在我的组合框中,我想将播放器名称显示为文本,将resourceId显示为值。

https://fifa15.content.easports.com/fifa/fltOnlineAssets/8D941B48-51BB-4B87-960A-06A61A62EBC0/2015/fut/items/web/players.json

1 个答案:

答案 0 :(得分:1)

为此你需要

http://james.newtonking.com/json

public class Player
{
    public int id { get; set; }
    public int r { get; set; }
    public int n { get; set; }
    public string f { get; set; }
    public string l { get; set; }
    public string c { get; set; }
}

public class LegendsPlayer
{
    public int id { get; set; }
    public int r { get; set; }
    public int n { get; set; }
    public string f { get; set; }
    public string l { get; set; }
    public string c { get; set; }
}

public class RootObject
{
    public List<Player> Players { get; set; }
    public List<LegendsPlayer> LegendsPlayers { get; set; }
}


// Then you load int your comboBox

private void LoadComboItems()
    {
        var str = File.ReadAllText(pathToYourFile);
        var x = JsonConvert.DeserializeObject<RootObject>(str);
        foreach (var player in x.Players)
        {
            ComboboxItem item = new ComboboxItem();
            item.Text = player.f + " " + player.l;
            item.Value = player.id;

            comboBox1.Items.Add(item);
        }        

    }