我正在尝试将转换后的字符串列表更新为JSON文件“列表”。
我的字符串列表:
var animals = new List<string>() { "bird", "dog" };
使用此代码:
string json = File.ReadAllText(filePath);
dynamic jsonObj = Newtonsoft.Json.JsonConvert.DeserializeObject(json);
var serializedObject = JsonConvert.SerializeObject(animals);
jsonObj["animals"] = serializedObject;
string output = Newtonsoft.Json.JsonConvert.SerializeObject(jsonObj, Newtonsoft.Json.Formatting.Indented);
File.WriteAllText(filePath, output);
旧的 JSON 文件:
{
"animals": ["cat", "fox"]
}
新的 JSON 文件应为:
{
"animals": ["bird", "dog"]
}
但我得到的是:
{
"animals": "[\"bird\", \"dog\"]"
}
任何帮助是值得赞赏的!
谢谢
翻过高山走不出你
慕码人8056858
相关分类