I have a valid JSON String that I want to tidy/format such that each property/value pair is on its own line, etc. (it currently is on one line w no spaces/line breaks).
I am using the Apache Sling JSONObject to model my JSON Object and turn it into a String, so if the Sling JSONObject can be set to output a tidy string (which I do not think it can) that would work too.
If i need a 3rd party lib i would prefer one w as few dependencies as possibles (such as Jackson which only requires the std JDK libs).
解决方案
You don't need an outside library.
public java.lang.String toString(int indentFactor)
throws JSONException
Make a prettyprinted JSON text of this JSONObject. Warning: This method assumes that the
data structure is acyclical.
Parameters:
indentFactor - The number of spaces to add to each level
of indentation.
Returns: a printable, displayable, portable,
transmittable representation of the object, beginning with { (left
brace) and ending with } (right brace).
Throws: JSONException - If the
object contains an invalid number.