2015-12-26 68 views
1

MY JSONC#的Json解析與Newtonsoft.Json

{"Players": [{"UserId": "76561198160689142"},{"UserId": "76561198160689123"},{"UserId": "76561198160689145"},{"UserId": "76561198160689144"}]} 

,我sucess上反序列化

JsonOxide obj = JsonConvert.DeserializeObject<JsonOxide>(jsonread); 


     foreach(var item in obj.Players) 
     { 
      MessageBox.Show(item.UserId); 
     } 

類JsonOxide.cs(我使用json2csharp使代碼)

public class Player 
{ 
    public string UserId { get; set; } 
} 

public class JsonOxide 
{ 
    public List<Player> Players { get; set; } 
} 

我想序列化我的Listview項目,所以我嘗試像這樣的代碼,但我不知道 如何序列化

 List<JsonOxide> jolist = new List<JsonOxide>(); 
     var objs = new Object[] 
     { 
     //wrong code 
     } 
     JsonOxide jo = new JsonOxide(); 

〔實施例

listivewitem 1.text = "test" 
listviewitem 2.text = "test2" 
will be save { "userid": "test"},{ "userid": "test2"} 

和 listivew項目,如果ListViewItem的1被刪除將編輯JSON 刪除用戶名:測試值是如何做到這一點?

回答

0

您可以使用SerializeObject方法。

string players = JsonConvert.SerializeObject(oxide); 

至於你所提供的結構,一個完整的例子:

JsonOxide oxide = new JsonOxide(); 
oxide.Players = new List<Player>(); 

oxide.Players.Add(new Player { UserId = "1" }); 
oxide.Players.Add(new Player { UserId = "2" }); 

string players = JsonConvert.SerializeObject(oxide); 

Console.WriteLine(players); 

會顯示:

{"Players":[{"UserId":"1"},{"UserId":"2"}]}