我能否在以下发言中发表一些意见?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作为您的 JSON 分析者, 那么这就是你如何 允许它 允许评论:

ObjectMapper mapper = new ObjectMapper().configure(Feature.ALLOW_COMMENTS, true);

然后,你可以有这样的评论:

{
  key: "value" // Comment
}

您也可以从#通过设定:

mapper.configure(Feature.ALLOW_YAML_COMMENTS, true);

但总的来说(如前所述),规格不允许作出评论。

是的,你可以,但你的分析 可能会失败(没有标准)

要分析这些注释,您应该删除这些注释,或者通过手写,或者使用正则表达式:

它取代任何评论,例如:

/****
 * Hey
 */

/\/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*\/+/

它取代任何评论,例如:

// Hey

/\/\/.*/

在JavaScript, 你可以做这样的事情:

jsonString = jsonString.replace(/\/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*\/+/, "").replace(/\/\/.*/,"")
var object = JSON.parse(jsonString);

缩略JSON 规格不支持评论,// or /* */样式。

但一些JSON分析图书馆和国际开发实体支持它们。

类似 :

  1. 杰森5
  2. 视觉工作室代码
  3. Commentjson 评论

如果您正在使用 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在本地没有评论。当然,有时你还是想要评论。Python 符号,两个方法commentjson (#//仅用于 Python 2 的)或json_tricks (#//Python 2 和 Python 3 的 Python 2 和 Python 3, 具有其他几个特点。json_tricks.