I just want to replace 2 areas AREA1,AREA2 inside the below json file. How can I retrieve the element with jObject.SelectTokens() and edit it? Thanks in advance.
{
"summary": {
"summaryDataTable": {
"id": "summaryDataTable",
"type": "dataTable",
"source": "/SmartScreen/SummaryFw",
"columns": [
{
"name": "CreateDate",
"title": "AREA1",
"filterable": false,
},
{
"name": "CreateUser",
"title": "AREA2",
"filterable": false,
},
],
"options": {
"showFilter": false,
}
}
}
}
CodePudding user response:
If you just need to modify it for the above JSON model, use JObject.SelectToken
to get the parent objects using the JSON path & then set the value like below:
var data = JObject.Parse(json);
var firstColumn = data.SelectToken("summary.summaryDataTable.columns[0]");
var secondColumn = data.SelectToken("summary.summaryDataTable.columns[1]");
firstColumn["title"] = "replacedTitle1";
secondColumn["title"] = "replacedTitle2";
Output:
{
"summary": {
"summaryDataTable": {
"id": "summaryDataTable",
"type": "dataTable",
"source": "/SmartScreen/SummaryFw",
"columns": [
{
"name": "CreateDate",
"title": "replacedTitle1",
"filterable": false
},
{
"name": "CreateUser",
"title": "replacedTitle2",
"filterable": false
}
],
"options": {
"showFilter": false
}
}
}
}
CodePudding user response:
You can try this
var json= ...your json;
var o= JObject.Parse(json);
var columns=o.SelectToken("summary.summaryDataTable.columns");
columns[0]["title"]="newTitle1";
columns[1]["title"]="newTitle2";
json = o.ToString(Newtonsoft.Json.Formatting.Indented);