我在下面有这个json文件。我已经下载了它。我想用这个json文件中的数据填充一个组合框。如何在win表单应用程序(c#)中执行此操作?
在我的组合框中,我想将播放器名称显示为文本,将resourceId显示为值。
答案 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);
}
}