可以在JSON中使用評論嗎?


Answers

,JSON中不允許使用表單//…/*…*/註釋。 這個答案基於:

  • http://www.json.org
  • RFC 4627 :用於JavaScript對象表示法(JSON)的application/json媒體類型
  • RFC 7159 JavaScript對象表示法(JSON)數據交換格式 - 廢止時間:4627,7158
Question

我可以在JSON文件中使用評論嗎? 如果是這樣,怎麼樣?




評論不是官方標準。 儘管一些解析器支持C風格的評論。 我使用的是JsonCpp 。 在這個例子中有這個:

// Configuration options
{
    // Default encoding for text
    "encoding" : "UTF-8",

    // Plug-ins loaded at start-up
    "plug-ins" : [
        "python",
        "c++",
        "ruby"
        ],

    // Tab indent size
    "indent" : { "length" : 3, "use_space": true }
}

jsonlint不驗證這一點。 所以註釋是一個解析器特定的擴展,而不是標準。

另一個解析器是JSON5

替代JSON TOML




JSON背後的想法是在應用程序之間提供簡單的數據交換。 這些通常是基於Web的,而語言是JavaScript。

它並不真正允許評論,但是,將評論作為數據中的名稱/值對之一傳遞肯定會起作用,儘管該數據顯然需要被解析代碼忽略或專門處理。

總而言之,JSON文件不應該包含傳統意義上的評論。 它應該只是數據。

查看JSON網站以獲取更多詳細信息。




If you use JSON5 you can include comments.

JSON5 is a proposed extension to JSON that aims to make it easier for humans to write and maintain by hand. It does this by adding some minimal syntax features directly from ECMAScript 5.




考慮使用YAML。 它幾乎是JSON的超集(幾乎所有有效的JSON都是有效的YAML),它允許評論。




JSON不支持評論。 它也從未打算用於需要評論的配置文件。

Hjson是人類的配置文件格式。 輕鬆的語法,更少的錯誤,更多的評論。

有關JavaScript,Java,Python,PHP,Rust,Go,Ruby和C#庫,請參見hjson.org




We are using strip-json-comments for our project. It supports something like:

/*
 * Description 
*/
{
    // rainbows
    "unicorn": /* ❤ */ "cake"
}

Simply npm install --save strip-json-comments to install and use it like:

var strip_json_comments = require('strip-json-comments')
var json = '{/*rainbows*/"unicorn":"cake"}';
JSON.parse(strip_json_comments(json));
//=> {unicorn: 'cake'}



JSON makes a lot of sense for config files and other local usage because it's ubiquitous and because it's much simpler than XML.

If people have strong reasons against having comments in JSON when communicating data (whether valid or not), then possibly JSON could be split into two:

  • JSON-COM: JSON on the wire, or rules that apply when communicating JSON data.
  • JSON-DOC: JSON document, or JSON in files or locally. Rules that define a valid JSON document.

JSON-DOC will allow comments, and other minor differences might exist such as handling whitespace. Parsers can easily convert from one spec to the other.

With regards to the plus.google.com/118095276221607585885/posts/RK8qyGVaGSr made by Douglas Crockford on this issues (referenced by @Artur Czajka)

Suppose you are using JSON to keep configuration files, which you would like to annotate. Go ahead and insert all the comments you like. Then pipe it through JSMin before handing it to your JSON parser.

We're talking about a generic config file issue (cross language/platform), and he's answering with a JS specific utility!

Sure a JSON specific minify can be implemented in any language, but standardize this so it becomes ubiquitous across parsers in all languages and platforms so people stop wasting their time lacking the feature because they have good use-cases for it, looking the issue up in online forums, and getting people telling them it's a bad idea or suggesting it's easy to implement stripping comments out of text files.

The other issue is interoperability. Suppose you have a library or API or any kind of subsystem which has some config or data files associated with it. And this subsystem is to be accessed from different languages. Then do you go about telling people: by the way don't forget to strip out the comments from the JSON files before passing them to the parser!




You can have comments in JSONP , but not in pure JSON. I've just spent an hour trying to make my program work with this example from Highcharts: http://www.highcharts.com/samples/data/jsonp.php?filename=aapl-c.json&callback=?

If you follow the link, you will see

?(/* AAPL historical OHLC data from the Google Finance API */
[
/* May 2006 */
[1147651200000,67.79],
[1147737600000,64.98],
...
[1368057600000,456.77],
[1368144000000,452.97]
]);

Since I had a similar file in my local folder, there were no issues with the Same-origin policy , so I decided to use pure JSON... and, of course, $.getJSON was failing silently because of the comments.

Eventually I just sent a manual HTTP request to the address above and realized that the content-type was text/javascript since, well, JSONP returns pure JavaScript. In this case comments are allowed . But my application returned content-type application/json , so I had to remove the comments.




設計中刪除了JSON的評論。

我從JSON中刪除了評論,因為我看到有人使用它們來保存解析指令,這種做法會破壞互操作性。 我知道缺乏評論會讓一些人傷心,但不應該。

假設您使用JSON來保存想要註釋的配置文件。 繼續並插入你喜歡的所有評論。 然後在將它交給JSON解析器之前通過JSMin管理它。

來源: plus.google.com/118095276221607585885/posts/RK8qyGVaGSr




我剛剛遇到這個配置文件。 我不想使用XML (冗長,圖形,醜陋,難以閱讀)或“ini”格式(沒有層次結構,沒有真正的標準等)或Java“屬性”格式(如.ini)。

JSON可以做所有他們可以做的事情,但它不那麼冗長,並且更具人類可讀性 - 並且解析器在許多語言中都很容易和無處不在。 這只是一個數據樹。 但是,帶外評論經常需要記錄“默認”配置等。 配置永遠不會是“完整文檔”,而是保存的數據樹,在需要時可以是人類可讀的。

我想可以使用"#": "comment" ,為“有效的”JSON。




You can use JSON with comments in it, if you load it as a text file, and then remove comments from it.

你可以使用它的decomment庫。下面是一個完整的例子。

輸入JSON(文件input.js):

/*
* multi-line comments
**/
{
    "value": 123 // one-line comment
}

測試應用:

var decomment = require('decomment');
var fs = require('fs');

fs.readFile('input.js', 'utf8', function (err, data) {
    if (err) {
        console.log(err);
    } else {
        var text = decomment(data); // removing comments
        var json = JSON.parse(text); // parsing JSON
        console.log(json);
    }
});

輸出:

{ value: 123 }

另請參閱:gulp-decommentgrunt-decomment




以下是我在Google Firebase文檔中找到的內容 ,可讓您將評論放入JSON:

{
  "//": "Some browsers will use this to enable push notifications.",
  "//": "It is the same for all projects, this is not your project's sender ID",
  "gcm_sender_id": "1234567890"
}



抱歉,我們無法在JSON中使用評論...請參閱JSON.org JSON語法圖。

道格拉斯克羅克福德說:“ plus.google.com/118095276221607585885/posts/RK8qyGVaGSr ”:

我從JSON中刪除了評論,因為我看到有人使用它們來保存解析指令,這種做法會破壞互操作性。 我知道缺乏評論會讓一些人傷心,但不應該。

假設您使用JSON來保存想要註釋的配置文件。 繼續並插入你喜歡的所有評論。 然後在將它交給JSON解析器之前通過JSMin管理它。




To cut a JSON item into parts I add "dummy comment" lines:

{

"#############################" : "Part1",

"data1"             : "value1",
"data2"             : "value2",

"#############################" : "Part2",

"data4"             : "value3",
"data3"             : "value4"

}



Links