【问题标题】:Correctly create a json file in c#在c#中正确创建一个json文件
【发布时间】:2021-03-26 20:23:26
【问题描述】:

我目前正在设置我的应用程序,我需要在我的 Windows 窗体中管理一个 json 文件(包含一些设置)。打开后,您可以轻松选择不同的设置,完成后,您可以保存它(这意味着我需要覆盖现有的一个 json 文件设置,并用新的设置替换它!


我尝试按照this 指南正确创建我的 json 文件!但我遇到了两个问题:

  1. 上面提到了这个解决方案,创建方括号(我不需要!)
  2. 似乎在一行上创建所有设置。这是正确的还是将来可能会出现问题?

一些材料......

我的申请:

原始json文件:

使用我的代码生成的 Json 文件:

我的班级:

    public class data
    {
        public bool avx { get; set; }
        public bool memory_pool { get; set; }
        public bool smt { get; set; }
        public bool spectre { get; set; }
        public bool unlock_menu { get; set; }
        public bool vinput { get; set; }
        public double cpu_memory_pool_fraction { get; set; }
        public double gpu_memory_pool_fraction { get; set; }
    }

我的代码:

private void btn_save_Click(object sender, EventArgs e)
{
    string settings_path = general_path + "\\plugins\\cyber_engine_tweaks\\" + "config.json"; //path

    bool avx_set = cb_avx.Checked;
    bool smt_set = cb_smt.Checked;
    bool memory_set = cb_memory.Checked;
    bool spectre_set = cb_spectre.Checked;
    bool debug_set = cb_debug.Checked;
    bool vinput_set = cb_vinput.Checked;

    List<data> _data = new List<data>();
    _data.Add(new data()
    {
        avx = avx_set,
        cpu_memory_pool_fraction = 0.5,
        gpu_memory_pool_fraction = 1.0,
        memory_pool = memory_set,
        smt = smt_set,
        spectre = spectre_set,
        unlock_menu = debug_set,
        vinput = vinput_set
    });

    using (StreamWriter file = File.CreateText(settings_path))
    {
        JsonSerializer serializer = new JsonSerializer();
        //serialize object directly into file stream
        serializer.Serialize(file, _data);
    }
}

【问题讨论】:

    标签: c# json class settings jsonserializer


    【解决方案1】:
    1. 方括号是因为你发送了一个数组:
      代替 List&lt;data&gt; _data = new List&lt;data&gt;(); _data.Add(new data()...
      尝试 var data = new data()... serializer.Serialize(file, data)

    2. 一行的所有设置都是正常的。

    【讨论】:

    • 嗨@Volodymyr_UA,编译器现在向我显示一个错误:"'data' is a variable but is used like a type"
    • @Zenek 我重新创建了您的代码,但没有发现错误。
    【解决方案2】:

    这里非常详细的答案:Can JSON start with "["?

    TLDR:

    1. 如果没有{} 表示对象或[] 表示数组,它就不是Json。所以不,你不能有一个没有多个键的 json。
    2. Json 中的换行符是可选的。由于大多数 json 对象用于通过网络传输,因此不需要换行符(占用不必要的字节)。

    【讨论】:

      【解决方案3】:

      问题: 1.上面提到的这个解决方案,创建方括号(我不需要!)

      解决方案:

      创建您的数据对象,如:

              //List<data> _data = new List<data>();
              data _data = new data 
              { 
                  avx = avx_set,
                  cpu_memory_pool_fraction = 0.5,
                  gpu_memory_pool_fraction = 1.0,
                  memory_pool = memory_set,
                  smt = smt_set,
                  spectre = spectre_set,
                  unlock_menu = debug_set,
                  vinput = vinput_set
              };
      

      2 。似乎在一行上创建所有设置。这是正确的还是将来可能会造成一些问题?

      解决方案: 我觉得是格式问题。可以忽略

      【讨论】:

      • 感谢@Presad Ramireddy 的回复!不幸的是,这个解决方案对我不起作用(或者我可能错过了什么?)编译器告诉我这个错误:The type or namespace name 'data' could not be found (are you missing a using directive or an assembly reference?)
      • 这里的“数据”是一个引用 tobject,它是您的“数据”类,即: public class data { public bool avx { get;放; } 公共布尔内存池 { 获取;放; } 公共布尔 smt { 获取;放; } 公共布尔幽灵 { 得到;放; } public bool unlock_menu { 得到;放; } 公共布尔 vinput { 获取;放; } 公共双 cpu_memory_pool_fraction { 得到;放; } 公共双 gpu_memory_pool_fraction { 得到;放; } }
      • 是的,我知道,但我不明白为什么在课程正确设置和工作时我仍然会收到此错误...您对此有任何想法吗?
      猜你喜欢
      • 1970-01-01
      • 2018-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-17
      • 1970-01-01
      • 2010-10-22
      • 2021-01-28
      相关资源
      最近更新 更多