LoginSignup
8
8

More than 5 years have passed since last update.

Gsonを使ってJSONのpretty print

Posted at

Gsonを使ってJSONのpretty print。ワンライナー風。

GsonJsonPP.java
package org.example.javalabo;

import com.google.gson.GsonBuilder;
import com.google.gson.JsonParser;

public class GsonJsonPP {
    public static void main(String[] args) {
        System.out.println(new GsonBuilder().setPrettyPrinting().create()
                .toJson(new JsonParser()
                        .parse("{\"aaa\":0, \"bbb\": [\"ddd\", \"eee\"], \"fff\":{\"ggg\":\"hhh\"}}")));
    }
}

こうなる。

出力
{
  "aaa": 0,
  "bbb": [
    "ddd",
    "eee"
  ],
  "fff": {
    "ggg": "hhh"
  }
}
8
8
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
8