【问题标题】:C# Newtonsoft cant modify array in JSON fileC# Newtonsoft 无法修改 JSON 文件中的数组
【发布时间】: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 文档,但找不到任何解决方案

【问题讨论】:

    标签: c# .net json.net


    【解决方案1】:

    你的类型转换是错误的。野兽方法是使用如下的 c# 对象。 为json数据创建一个类

    public class jsonData
        {
            public string UplayExecutable { get; set; }
    
            public string foo { get; set; }
            public int OpeningTime { get; set; }
    
            public string[] accounts { get; set; }
        }
    

    现在,编写读取 json 文件的代码,更新帐户值并写回文件。

                string json = File.ReadAllText("data.json");
                var jsonObj = Newtonsoft.Json.JsonConvert.DeserializeObject<jsonData>(json);
    
                string[] accs = { "hi@hi.com , testpass", "hey@hey.com , abcd123", "hello@hello.com , hi123" };
    
                jsonObj.accounts = accs;
    
                var jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(jsonObj, Newtonsoft.Json.Formatting.Indented);
                File.WriteAllText("data.json", jsonString);
    

    【讨论】:

      【解决方案2】:

      是的,您不能像修复它的方式将字符串 [] 隐式转换为 JContainer,方法是通过在内部分配带有字符串 [] 值的 JArray。这是对您的代码的修复

      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"] = new JArray(accs);
      string output = Newtonsoft.Json.JsonConvert.SerializeObject(jsonObj, Newtonsoft.Json.Formatting.Indented);
      
      File.WriteAllText("data.json", output);
      

      但是我们在这里不需要使用动态类型,我们可以只解析 JSON,替换数组然后取回 json。像这样

      string json = File.ReadAllText("data.json");
      var jObject = JObject.Parse(json);
      string[] accs = { "hi@hi.com , testpass", "hey@hey.com , abcd123", "hello@hello.com , hi123" };
      jObject["accounts"] = new JArray(accs);
      
      File.WriteAllText("data.json", jObject.ToString(Newtonsoft.Json.Formatting.Indented) );
      

      【讨论】:

        猜你喜欢
        • 2013-09-05
        • 2019-12-12
        • 1970-01-01
        • 1970-01-01
        • 2021-10-23
        • 1970-01-01
        • 2020-07-20
        • 1970-01-01
        • 2021-10-13
        相关资源
        最近更新 更多