从json文件中读取数据并设置为combobox项
本文关键字:设置 combobox 数据 json 文件 读取 | 更新日期: 2023-09-27 18:28:51
下面有这个json文件。我已经下载了它。我想用这个json文件中的数据填充一个组合框。如何在获胜表格应用程序(c#)中做到这一点?
在我的组合框中,我想将playername显示为文本,将resourceId显示为值。
https://fifa15.content.easports.com/fifa/fltOnlineAssets/8D941B48-51BB-4B87-960A-06A61A62EBC0/2015/fut/items/web/players.json
为此,您需要
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);
}
}