Java Json主观漂亮印刷

时间:2018-08-15 08:29:27

标签: java json string syntax gson

使用以下内容,我的gson构建器将打印以下内容:

try (Writer writer = Files.newBufferedWriter(Paths.get(path + ".json"), StandardCharsets.UTF_8)) {
        new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create().toJson(item, writer);
    } catch (Exception e) {
        logger.log(e);
    }

导致:

"dharoks": {
    "inventory": [
      {
        "id": 4718.0,
        "amount": 1.0
      },
      {
        "id": 15272.0,
        "amount": 1.0
      },
      {
        "id": 23279.0,
        "amount": 1.0
      },
      {
        "id": 23255.0,
        "amount": 1.0
      },
      {
        "id": 15272.0,
        "amount": 1.0
      },
      {
        "id": 15272.0,
        "amount": 1.0
      }
    ]

如何使它如此漂亮地打印只能美化某些数据,即如何使结果像这样:

"dharoks": {
    "inventory": [
      { "id": 23279.0, "amount": 1.0 },
      { "id": 23255.0, "amount": 1.0 },
      { "id": 15272.0, "amount": 1.0 },
      { "id": 15272.0, "amount": 1.0 },
    ]

我只希望Object(id,amount)不使用漂亮的打印效果(换行符除外),

0 个答案:

没有答案