How can you create a pretty-printed JSON output in GSON?
A) By using GsonBuilder().setPrettyPrinting()
B) By using the @SerializedName annotation
C) By overriding the toString() method
D) By setting a flag in the JSON
Answer:
A) By using GsonBuilder().setPrettyPrinting()
Explanation:
In GSON, you can create a pretty-printed JSON output by using the GsonBuilder().setPrettyPrinting()
method. This option formats the JSON output with indentation and line breaks, making it easier to read.
For example:
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String json = gson.toJson(someObject);
In this example, the setPrettyPrinting()
method ensures that the resulting JSON string is formatted in a human-readable way, with each element on a new line and properly indented.