C# Json Parse with Newtonsoft.Json

本文关键字:Json Newtonsoft with Parse | 更新日期: 2023-09-27 18:28:10

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

列出不包括的项目

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

以及如果删除Listviewitem 1,将编辑jsondelete userid:测试值怎么做?

C# Json Parse with Newtonsoft.Json

您可以使用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"}]}