C# Newtonsoft JSON Update Where

本文关键字:Where Update JSON Newtonsoft | 更新日期: 2023-09-27 18:05:10

我想用where select之类的东西更新JToken值。

JSON:
[
  {
    "Uid": "7775",
    "MessageID": "<c86bee87-ad2f-ebe6-5f62-1879faf76716@xxx.de>",
    "Mailbox": "INBOX",
    "Subject": "asdfdsf",
    "Seen": true,
    "Date": "31.08.2016 17:24:42",
    "Name": "asdasdasd",
    "Attachments": "0"
  },
  {
    "Uid": "7776",
    "MessageID": "<d0a1ec7a-7ca4-b5b6-7d1f-968e42e262d4@xxx.de>",
    "Mailbox": "INBOX",
    "Subject": "sdfdsf",
    "Seen": true,
    "Date": "31.08.2016 17:35:05",
    "Name": "asdfasdf",
    "Attachments": "0"
  },
  {
    "Uid": "7777",
    "MessageID": "<8d74bd7e66889c2a423ce43d032fc1ce@xxx.net>",
    "Mailbox": "INBOX",
    "Subject": "xxxs",
    "Seen": true,
    "Date": "31.08.2016 17:47:24",
    "Name": "xxxssw",
    "Attachments": "0"
  }
]

我试过这样的东西,但我找不到任何函数更新:[!!伪代码!!)

JToken storage = JToken.Parse(System.IO.File.ReadAllText("tmp''mail''index.txt"));
storage = storage.FirstOrDefault(o => (string)o["seen"] == "false").Update("seen", "true"); // Update(Field, NewValue)
storage.WriteToFile(...)

C# Newtonsoft JSON Update Where

JArray array = JArray.Parse(yourJson);
JObject jo = (JObject)array.FirstOrDefault(token => !(bool)token["Seen"]);
// you could make an extension method for the remove/add sequence
jo.Property("Seen").Remove();
jo.Property("Subject").AddAfterSelf(new JProperty("Seen", true));
// write back to file array.ToString()