【问题标题】:Stripping down C-like comments from otherwise legal json从其他合法的 json 中去除类似 C 的注释
【发布时间】:2013-06-25 09:34:31
【问题描述】:

假设你有一个包含 C 风格 cmets 的 json 文件

{
  "foo": {
    "default_level": "debug",
    // A comment
    "impl": "xyz"
  },
  "bar": [
    {
      /*This is a comment*/
      "format": "%l%d %c ….",
      "rotation": "daily, 1_000_000",
    }
  ]
}

在反序列化 json 之前,使用 Java 剥离这些 cmets 最简单的方法是什么?假设只支持单行 // 和多行 /**/ cmets。

最后,我想读入同一文件的 String 表示,但没有 cmets:

{
  "foo": {
    "default_level": "debug",
    "impl": "xyz"
  },
  "bar": [
    {
      "format": "%l%d %c ….",
      "rotation": "daily, 1_000_000",
    }
  ]
}

【问题讨论】:

  • 你几乎有 10k 代表,你应该知道你应该展示你已经尝试或研究过的东西。如果你不想自己做,我会花 15 美元/小时,一次性完成:P
  • C示例如何有效json?分隔符必须是换行符。
  • 您的 json 能否将注释字符作为数据,例如“comment”:“/* 这是数据 */”??
  • 这当然不是合法的 JSON。
  • @HotLicks 是的,明白了

标签: java json parsing


【解决方案1】:

将其作为 Javascript 处理可能会更好,因为 JSON 几乎是 Javascript 的一个子集,而 JSON + C-like cmets 实际上几乎是 Javascript 的一个子集。试试:

Looking to remove comments from a large amount of javascript files

基本上 - 只需先通过您最喜欢的缩小器运行它。请注意JSON is not a strict subset of Javascript,因此您需要将几乎合法的 JSON 塞入合法的 Javascript 中,然后才能信任压缩程序。幸运的是,这可以通过简单的查找和替换来解决。

【讨论】:

    【解决方案2】:

    实际上是一个不平凡的问题。我个人建议 IMO 在这方面做得很好的 Comment-Stripper 库。在这里找到:https://github.com/Slater-Victoroff/CommentStripper?source=cc

    更完整的功能和调试版本是前一段时间分叉的,但希望能解决这个问题。

    完全披露:我在问了一个类似的问题并意识到我找不到任何好的解决方案后编写了这个库。

    如果您只是想删除 cmets,我相信您可以在 Python 中轻松完成,您可以使用 Jython 调用它。

    import json
    return json.dumps(json.loads("file.json"))
    

    如果您对 Native Java 死心塌地,您可以使用 GSON 来做基本相同的事情。 (http://code.google.com/p/google-gson/),我认为 Jackson (http://jackson.codehaus.org/) 也可以,不过我建议使用更轻的 GSON 来完成这么简单的事情。

    GSON 示例:

    Gson gson = new Gson();
    BufferedReader br = //BufferedReader for your source;
    String clean = gson.toJson(gson.fromJson(br, Class.class))
    

    示例给出的理解是有一些支持代码需要配合,本示例仅封装了GSON的使用。其余的应该很简单(创建一个泛型类型类),如果您真的遇到问题,请查看 GSON 文档。

    https://sites.google.com/site/gson/gson-user-guide

    【讨论】:

    • 您能否举例说明如何使用gson 完成此操作?
    【解决方案3】:

    试试这个正则表达式。

    String jsonData =
    "{\n"+
    "  \"foo\": {\n"+
    "    \"default_level\": \"debug\",\n"+
    "    // A comment\n"+
    "    \"impl\": \"xyz\"\n"+
    "  },\n"+
    "  \"bar\": [\n"+
    "    {\n"+
    "      /*This is a comment*/\n"+
    "      \"format\": \"%l%d %c ….\",\n"+
    "      /* This is a\n"+
    "         multi-line comment */\n"+
    "      \"rotation\": \"daily, 1_000_000\",\n"+
    "    }\n"+
    "  ]\n"+
    "}";
    
    System.out.println(
           jsonData.replaceAll("//.*\\n\\s*|/\\*.*?\\n?.*?\\*/\\n?\\s*", "")
    );
    

    输出:

    {
      "foo": {
        "default_level": "debug",
        "impl": "xyz"
      },
      "bar": [
        {
          "format": "%l%d %c ….",
          "rotation": "daily, 1_000_000",
        }
      ]
    }
    

    注意:如果您的 json 可以将注释字符作为数据,这将不起作用

     "comment":"/* this is data */", "impl": "abc//xyz"
    

    【讨论】:

    • 这也会删除像"impl": "abc//xyz"这样的json内容
    • 现在你有两个问题。
    • @djechlin 你能详细说明一下吗?
    • 正则表达式不是解决方案,-1
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-02
    • 2012-02-11
    • 2015-05-10
    • 1970-01-01
    • 1970-01-01
    • 2018-07-21
    • 2011-11-15
    相关资源
    最近更新 更多