我有以下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对象。有人可以给我任何方法的想法吗。
谢谢
答案 0 :(得分:2)
您可以执行以下操作。
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": []
}