User-832373396 posted
Hi progy,
Sir, based on your requirement, I tested a simple working example :
(note: the only difference is that JSON string is in a text file and your JSON stored at database)
TextFile1.txt:
{
"data":[
{
"ID":"1",
"AuthorUserID":"1236645",
"Replies":"8e5s22t",
"DeletePost":"no"
},
{
"ID":"2",
"AuthorUserID":"1236645",
"Replies":"8e5s22t",
"DeletePost":"no"
}
]
}
Then code:
string jsonStr= System.IO.File.ReadAllText(Path.Combine(@"C:\Users\v\Source\Repos\MVC","TextFile1.txt"));
JObject josnObject= JObject.Parse(jsonStr);
//josnObject["data"] and josnObject.First.First() is the same effort: {[ { "ID": "1", "AuthorUserID": "1236645", "Replies": "8e5s22t", "DeletePost": "no" }, { "ID": "2", "AuthorUserID": "1236645", "Replies": "8e5s22t", "DeletePost": "no" }]}
var json1=josnObject["data"] ;
var json2 = josnObject.First.First().ToList().Where(a=>a["ID"].ToString()=="2").FirstOrDefault()
Now json2:
{{
"ID": "2",
"AuthorUserID": "1236645",
"Replies": "8e5s22t",
"DeletePost": "no"
}}
Then josnObject.First.First().ToList().Where(a=>a["ID"].ToString()=="2").FirstOrDefault()["DeletePost"]="yes"
Now, it changed: josnObject.ToString();
So, full code:
string jsonStr= System.IO.File.ReadAllText(Path.Combine(@"C:\Users\v\Source\Repos\MVC","TextFile1.txt"));
JObject josnObject= JObject.Parse(jsonStr);
josnObject.First.First().ToList().Where(a=>a["ID"].ToString()=="2").FirstOrDefault()["DeletePost"]="yes";
string newJson=josnObject.ToString();
newJson Result:
{
"data": [
{
"ID": "1",
"AuthorUserID": "1236645",
"Replies": "8e5s22t",
"DeletePost": "no"
},
{
"ID": "2",
"AuthorUserID": "1236645",
"Replies": "8e5s22t",
"DeletePost": "yes"
}
]
}
With regards, Angelina Jolie