我能否在以下发言中发表一些意见?Json Json (Json Json) (Json Json Json) (Json Json) Json Json (Json Json) Json (Json) Json (Json) Json (Json) Json (Json) Json (Json) Json (Json) Json(Json) Json (Json) Json (Json) Json(Json) Json (Json) Json如果有,如何处理?


当前回答

*.json文件通常用作配置文件或静态数据,因此需要评论 * NetBeans等编辑在 *.json中接受评论。

问题在于将内容解析到对象上。 解决方案是总是应用清洁功能( 服务器或客户端 ) 。

普 普 普 尔

 $rgx_arr = ["/\/\/[^\n]*/sim", "/\/\*.*?\*\//sim", "/[\n\r\t]/sim"];
 $valid_json_str = \preg_replace($rgx_arr, '', file_get_contents(path . 'a_file.json'));

贾瓦史克里普特

valid_json_str = json_str.replace(/\/\/[^\n]*/gim,'').replace(/\/\*.*?\*\//gim,'')

其他回答

您可以使用JSON-LD (JSON-LD)schema.org 注释用于正确写入备注:

{
    "https://schema.org/comment": "this is a comment"
}

使用杰森5您可以包含注释。


JSON5建议延长JSON5的有效期这样做的方式是直接从ECMAScript 5中增加一些最低限度的语法特征。

如果您正在使用 PHP, 您可以使用此函数搜索并删除 JSON 字符串中的 / / / * 键入批注, 然后将其解析成对象/ 数组 :

function json_clean_decode($json, $assoc = true, $depth = 512, $options = 0) {
       // search and remove comments like /* */ and //
       $json = preg_replace("#(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/)|([\s\t]//.*)|(^//.*)#", '', $json);

       if(version_compare(phpversion(), '5.4.0', '>=')) {
           $json = json_decode($json, $assoc, $depth, $options);
       }
       elseif(version_compare(phpversion(), '5.3.0', '>=')) {
           $json = json_decode($json, $assoc, $depth);
       }
       else {
           $json = json_decode($json, $assoc);
       }

       return $json;
   }

希望这有帮助!

你不行,至少这就是我的经验json.org.

JSON的语法在那一页上被视觉化了, 没有任何关于评论的注释。

是的,你可以发表意见,但我建议不提出上述任何理由。

我做了一些调查, 我发现所有JSON需要的方法 使用JSON.parse方法。所以我找到了一个解决方案: 我们可以在JSON.parse周围取代或修补猴子。

注:仅用节点js测试;-)

var oldParse = JSON.parse;
JSON.parse = parse;
function parse(json){
    json = json.replace(/\/\*.+\*\//, function(comment){
        console.log("comment:", comment);
        return "";
    });
    return oldParse(json)
}

JSON 文件 :

{
  "test": 1
  /* Hello, babe */
}