相关疑难解决方法(0)

可以在JSON中使用注释吗?

我可以在JSON文件中使用注释吗?如果是这样,怎么样?

json comments

7104
推荐指数
46
解决办法
203万
查看次数

如何通过Json.NET评论阅读json

为了在Google Chrome浏览器中安装外部扩展程序,我尝试更新chrome外部扩展json文件.使用Json.NET它似乎很容易:

string fileName = "..."; // path to chrome external extension json file

string externalExtensionsJson = File.ReadAllText(fileName);

JObject externalExtensions = JObject.Parse(externalExtensionsJson);
Run Code Online (Sandbox Code Playgroud)


但我得到一个Newtonsoft.Json.JsonReaderException说法:

"Error parsing comment. Expected: *, got /. Path '', line 1, position 1." 
Run Code Online (Sandbox Code Playgroud)


在调用时,JObject.Parse因为此文件包含:

// This json file will contain a list of extensions that will be included
// in the installer.

{
}
Run Code Online (Sandbox Code Playgroud)

和注释不是json的一部分(如何在Json.NET输出中添加注释?).

我知道我可以用正则表达式删除注释(正则表达式删除javascript双斜杠(//)样式注释)但我需要在修改后将json重写到文件中并保持评论可能是一个很好的想法.

问题:有没有办法在没有删除它们的情况下读取带有注释的json并能够重写它们?

c# json.net

37
推荐指数
1
解决办法
1万
查看次数

标签 统计

c# ×1

comments ×1

json ×1

json.net ×1