如何使用Json.NET读取带有注释的JSON内容? [英] How can I read JSON content with a comment with Json.NET?

查看:163
本文介绍了如何使用Json.NET读取带有注释的JSON内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了将外部扩展安装到Google Chrome浏览器中,我尝试更新Chrome外部扩展JSON文件.使用Json.NET似乎很容易:

In order to install an external extension into Google Chrome browser, I try to update a Chrome external extension JSON file. Using Json.NET it seems to be easy:

string fileName = "..."; // Path to a Chrome external extension JSON file

string externalExtensionsJson = File.ReadAllText(fileName);

JObject externalExtensions = JObject.Parse(externalExtensionsJson);


但是我得到一个Newtonsoft.Json.JsonReaderException话:

"Error parsing comment. Expected: *, got /. Path '', line 1, position 1."


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


when calling JObject.Parse because this file contains:

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

{
}

注释不是JSON的一部分(如

And comments are not part of JSON (as seen in How do I add comments to Json.NET output?).

我知道我可以使用正则表达式(

I know I can remove comments with a regular expression (Regular expression to remove JavaScript double slash (//) style comments), but I need to rewrite JSON into the file after modification and keeping comment can be a good thing.

有没有一种方法可以读取带注释的JSON内容而不删除它们并能够重写它们?

Is there a way to read JSON content with comments without removing them and be able to rewrite them?

推荐答案

Json.NET仅支持阅读多行JavaScript注释,即/*注释*/

Json.NET only supports reading multi-line JavaScript comments, i.e. /* commment */

更新:Json.NET 6.0支持单行注释

Update: Json.NET 6.0 supports single line comments

这篇关于如何使用Json.NET读取带有注释的JSON内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆