【发布时间】:2021-07-03 12:52:09
【问题描述】:
我有一个来自此架构的 JSON 文件。 这有一个称为帐户的数组。
{
"UplayExecutable": "C:\\Program Files (x86)\\Ubisoft\\Ubisoft Game Launcher\\UbisoftConnect.exe",
"foo": "barr",
"OpeningTime": 8,
"accounts": [
"abc@a.com , testpass",
"rumirad@gmail.com , password2",
"rumirad@outlook.com , password3",
"rumirad@test.com , password3"
]
}
我正在使用 .NET 框架。我也使用 newtonsoft 库。我想用 C# String 数组替换那个数组。 字符串数组是硬编码的,因为我想在这里测试它。 这没有编译错误。存在运行时错误。
private void button1_Click(object sender, EventArgs e)
{
string json = File.ReadAllText("data.json");
dynamic jsonObj = Newtonsoft.Json.JsonConvert.DeserializeObject(json);
string[] accs = { "hi@hi.com , testpass", "hey@hey.com , abcd123", "hello@hello.com , hi123" };
jsonObj["accounts"] = accs;
string output = Newtonsoft.Json.JsonConvert.SerializeObject(jsonObj, Newtonsoft.Json.Formatting.Indented);
File.WriteAllText("data.json", output);
}
我想知道如何将字符串数组作为json数组写入文件
这是错误。我阅读了 newtonsoft 文档,但找不到任何解决方案
【问题讨论】: