C#修改json文件中的某些值

时间:2023-03-08 20:26:51
C#修改json文件中的某些值
using Newtonsoft.Json;

JsonSerializer serialiser = new JsonSerializer();
string newContent = string.Empty;
using (StreamReader reader = new StreamReader(file.FullName))
{
string json = reader.ReadToEnd(); dynamic jsonObj = JsonConvert.DeserializeObject(json);
jsonObj["course/course"]["tabs"][0]["name"] = "Courseware";
jsonObj["course/course"]["tabs"][0]["type"] = "courseware"; jsonObj["course/course"]["tabs"][1]["name"] = "Course Info";
jsonObj["course/course"]["tabs"][1]["type"] = "course_info";
if (jsonObj["course/course"]["lti_passports"] != null)
{
for (int i = 0; i < jsonObj["course/course"]["lti_passports"].Count; i++)
{
string value = jsonObj["course/course"]["lti_passports"][i].ToString();
if (value.Contains("xtreme"))
{
jsonObj["course/course"]["lti_passports"][i] = "test:LeX:test";
}
}
} for(int i =0; i< jsonObj["course/course"]["advanced_modules"].Count; i++)
{
AdvancedModules.Add(jsonObj["course/course"]["advanced_modules"][i].ToString());
}
jsonObj["course/course"]["catalog_visibility"] = "none"; newContent = JsonConvert.SerializeObject(jsonObj, Newtonsoft.Json.Formatting.Indented);