2014-11-03 85 views

回答

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

    } 
+0

非常感謝你,我只有一個問題,類型ComboboxItem並不在我的winform applicatlion存在。我錯過了什麼嗎? – Jax 2014-11-03 15:59:01

+0

您需要導入控件。我展示了一種簡單的方法 – dariogriffo 2014-11-03 17:12:25

+0

奇怪我認爲組合框項目來自System.Windows.Controls,但我似乎沒有在我的正常Windows窗體應用程序。 – Sam 2015-06-28 13:12:19