C#Json Parse与Newtonsoft.Json

时间:2015-12-26 14:51:57

标签: c# json

我的JSON

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

我已成功反序列化

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

listivew item exmaple

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

和 如果Listviewitem 1被删除将编辑json 删除userid:test value怎么做?

1 个答案:

答案 0 :(得分: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"}]}