>我有以下JSON
{
"mode": 1,
"renderTo": 3,
"filterFields": [ 206 ],
"filters": [
{
"_subscriptions": [],
"RowState": 2,
"fieldID": 206,
"name": "Created Date",
"value1": "01/01/2019",
"value2": "01/03/2019",
"preEdit": null,
"postEdit": null
}
],
"priceCrossTabulation": 2,
"adHocFilterData": "",
"displayTemplate": "runTemplate",
"errors": [],
"visibleErrors": []
}
我想修改 JSON 对象中的这两个值
"value1": "01/01/2019",
"value2": "01/03/2019",
我能够使用以下代码获取值
var data1 = JObject.Parse(modifiedJsonString);
var data2 = data1.GetValue("filters").Values("value1").First();
data2 = "06/02/2018";
但不确定如何在 data1 对象中将其更新回来。有人可以给我任何关于如何做到这一点的想法。
谢谢
您可以执行以下操作。
JObject root = JObject.Parse(json);
JArray item = (JArray)root["filters"];
item[0]["value1"] = "02/02/2019"; // new value
item[0]["value2"] = "02/02/2019"; // new value
请注意filters
是一个数组,需要通过索引访问。
输出
{
"mode": 1,
"renderTo": 3,
"filterFields": [
206
],
"filters": [
{
"_subscriptions": [],
"RowState": 2,
"fieldID": 206,
"name": "Created Date",
"value1": "02/02/2019",
"value2": "02/02/2019",
"preEdit": null,
"postEdit": null
}
],
"priceCrossTabulation": 2,
"adHocFilterData": "",
"displayTemplate": "runTemplate",
"errors": [],
"visibleErrors": []
}