update json object in c# -
i have json object like
{ "name" : "sai", "age" : 22, "salary" : 25000}
i want update json object by
`{ "name" : "sai", "age" : 23, "gender" : "male"}`
then want result
{ "name" : "sai", "age" : 23, "salary" : 25000, "gender" : "male"}
i tried like
foreach(var item in actualjson) { bool isfound = false; foreach(var newitem in newjson) { if(item == newitem) // returns false, wrong this? { isfound = true; break; } } if(!isfound) { // add json } }
i not getting idea solve this?
any help/guidance appreciated.
with json.net can this:
var json1 = "{ \"name\" : \"sai\", \"age\" : 22, \"salary\" : 25000}"; var json2 = "{ \"name\" : \"sai\", \"age\" : 23, \"gender\" : \"male\"}"; var object1 = jobject.parse(json1); var object2 = jobject.parse(json2); foreach (var prop in object2.properties()) { var targetproperty = object1.property(prop.name); if (targetproperty == null) { object1.add(prop.name, prop.value); } else { targetproperty.value = prop.value; } } var result = object1.tostring(formatting.none);
this either add property of json2 json1 if doesn't exist or update value if exists.
Comments
Post a Comment