Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我在更新json文件中的值时遇到了双引号问题_Json_Parsing_Key_Updating - Fatal编程技术网

我在更新json文件中的值时遇到了双引号问题

我在更新json文件中的值时遇到了双引号问题,json,parsing,key,updating,Json,Parsing,Key,Updating,我正在读取一个json文件,它是3D切片器引擎的配置文件。我正在使用以下代码(摘要,不完整) 我正在从文件中读取特定值,并将该值(RGB值)放入文本框中,并在图片框中显示找到的颜色: Dim read = Newtonsoft.Json.Linq.JObject.Parse(json) col = read.Item("colors")("layerview_inset_0").ToString col = Replace(col, &

我正在读取一个json文件,它是3D切片器引擎的配置文件。我正在使用以下代码(摘要,不完整)

我正在从文件中读取特定值,并将该值(RGB值)放入文本框中,并在图片框中显示找到的颜色:

Dim read = Newtonsoft.Json.Linq.JObject.Parse(json)

col = read.Item("colors")("layerview_inset_0").ToString
        
col = Replace(col, "[", "")
        
col = Replace(col, "]", "")
        
col = Trim(col)
        
TextBox1.Text = Trim(Replace(col, " ", ""))
        
cols = Split(col, ",")
        
PictureBox1.BackColor = Color.FromArgb(cols(0), cols(1), cols(2))
这一切都很好

我接下来要做的是通过单击picturebox并选择不同的颜色来加载颜色选择器:

With ColorDialog1
            
'Get the current color from the picturebox
            
.Color = ColorDialog1.Color
            
If .ShowDialog = DialogResult.OK Then
                
Me.PictureBox1.BackColor = .Color
                
TextBox1.Text = .Color.R.ToString & "," & .Color.G.ToString & "," & .Color.B.ToString & "," & "255"
                
code = "[" & .Color.R.ToString & "," & .Color.G.ToString & "," & .Color.B.ToString & "," & "255" & "]"
            
End If
        
End With
这将创建一个字符串值(代码),然后用它替换文件中的键:

jsonObj("colors")("layerview_inset_0") = code
然后写回更新的文件

Dim output As String = Newtonsoft.Json.JsonConvert.SerializeObject(jsonObj, Newtonsoft.Json.Formatting.Indented)

File.WriteAllText("C:\tmp\theme.json", output)
这也很好,但插入的值用双引号括起来

文件中的原始值如下所示:

 "layerview_inset_0": [ 
      255,
      255,
      255,
      255
      ],
但在我更新后,它是:

 "layerview_inset_0": "[0,0,160,255]",
这会导致程序在加载文件时失败。 如果我删除双引号,它就正常工作了。 我以前从未使用过json文件,而且花了很长时间才做到这一点。如何将所需的数据添加到json文件中,而不使用双引号将其包装? 我认为这是因为我将值作为字符串插入,但我不知道其他方法,因为我需要确保我还添加了[和]以及每个RGB值,就像原始格式一样

除了在文件中的新值周围添加双引号外,所有操作都很正常,但是如何将这些数据添加到文件中而不发生这种情况呢? 我希望您能给我一些指导,请记住,这是我第一次尝试解析json文件并替换键值,所以请耐心点。我可能全做错了

 "layerview_inset_0": "[0,0,160,255]",