0
我想更新一個類似where select的JToken值。C#Newtonsoft JSON更新Where
JSON:
[
{
"Uid": "7775",
"MessageID": "<[email protected]>",
"Mailbox": "INBOX",
"Subject": "asdfdsf",
"Seen": true,
"Date": "31.08.2016 17:24:42",
"Name": "asdasdasd",
"Attachments": "0"
},
{
"Uid": "7776",
"MessageID": "<[email protected]>",
"Mailbox": "INBOX",
"Subject": "sdfdsf",
"Seen": true,
"Date": "31.08.2016 17:35:05",
"Name": "asdfasdf",
"Attachments": "0"
},
{
"Uid": "7777",
"MessageID": "<[email protected]>",
"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(...)
也許你可以deserialise的JSON變成某種對象列表,它實現IQueriable。 然後,您可以使用LINQ和重新列入JSON的reserialise來剔除列表或結果。 – AntDC
查看http://www.newtonsoft.com/json/help/html/ModifyJson.htm – dbc