I am having json string like {"name":"studentname","Lastname":"lastnameofstudent"} i want to change name property/key to FirstName not a value of this property. using Newtonsoft json lib.
Example:
string json = @"{
'name': 'SUSHIL'
}";
JObject obj = JObject.Parse(json);
var abc = obj["name"];
obj["name"] = "FirstName";
string result = obj.ToString();
The simplest way is probably just to assign a new property value, then call Remove
for the old one:
using System;
using Newtonsoft.Json.Linq;
class Test
{
static void Main()
{
string json = "{ 'name': 'SUSHIL' }";
JObject obj = JObject.Parse(json);
obj["FirstName"] = obj["name"];
obj.Remove("name");
Console.WriteLine(obj);
}
}
Output:
{
"FirstName": "SUSHIL"
}
See more on this question at Stackoverflow