我有以下Json对象
[
{
"ID": 21,
"Name": "Clement Rakesh",
"DOB": "1983-07-07T00:00:00",
"DOJ": "2014-07-30T00:00:00",
"Address": "BANGALORE",
"Mobile": 9611795983.0,
},
{
"ID": 34,
"Name": "Clement Rakesh",
"DOB": "0001-01-01T00:00:00",
"DOJ": "0001-01-01T00:00:00",
"Address": "BANGALORE",
"Mobile": 0.0,
}
]
到目前为止,我所做的是:
using (StreamReader sr = new StreamReader(FILENAME))
{
string s = sr.ReadToEnd();
JToken v = JArray.Parse(s);
var emp = v.Where(x => x["ID"].ToString() ==empmodl.ID.ToString()).ToList();
}
其中FILENAME是包含上述JSON对象的文件的路径。 emp包含要更新的必需对象。 现在我需要更新原始JSON对象中的emp信息。 有人可以帮忙吗?
答案 0 :(得分:1)
我建议如下:
var input = s; //step 1 dynamic yourJson = JObject.Parse(input); //step 2 yourJson[0].Name = "test"; //step 3 string updatedJson = yourJson.ToString();
答案 1 :(得分:0)
为什么不直接将整个JSON解析为对象,修改所需的一切并再次序列化?
using (StreamReader sr = new StreamReader(FILENAME))
{
string s = sr.ReadToEnd();
dynamic jsonObject = JObject.Parse(s);
var emp = v.Where(x => x["ID"].ToString() ==empmodl.ID.ToString()).ToList();
//do the modifications here
//write the contents to the file by using this
JsonConvert.SerializeObject(jsonObject);
}