Created
January 23, 2017 13:25
-
-
Save rahulrvp/d10d6bea0e0b87883da5b8ce21f96c81 to your computer and use it in GitHub Desktop.
A util class that helps your format the the JSON text using Java.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.json.JSONArray; | |
import org.json.JSONException; | |
import org.json.JSONObject; | |
import java.util.Iterator; | |
/** | |
* @author Rahul Raveendran V P | |
* Created on 23/1/17 @ 5:27 PM | |
* https://github.com/rahulrvp | |
*/ | |
public class JsonUtils { | |
public static String formatJson(String text) { | |
StringBuilder stringBuilder = new StringBuilder(); | |
try { | |
formatJson(stringBuilder, 0, new JSONObject(text)); | |
} catch (JSONException e) { | |
e.printStackTrace(); | |
} | |
return stringBuilder.toString(); | |
} | |
public static void formatJson(StringBuilder builder, int level, JSONObject jsonObject) throws JSONException { | |
if (jsonObject != null && builder != null) { | |
builder | |
.append(tabs(level)) | |
.append("{"); | |
Iterator<String> keys = jsonObject.keys(); | |
while (keys.hasNext()) { | |
builder.append("\n"); | |
String key = keys.next(); | |
Object o = jsonObject.get(key); | |
builder | |
.append(tabs(level + 1)) | |
.append("\"") | |
.append(key) | |
.append("\"") | |
.append(" : "); | |
if (o instanceof JSONArray) { | |
formatJson(builder, level + 1, (JSONArray) o); | |
} else if (o instanceof JSONObject) { | |
formatJson(builder, level + 1, (JSONObject) o); | |
} else if (o instanceof String) { | |
builder | |
.append("\"") | |
.append(o) | |
.append("\""); | |
} else { | |
builder.append(o); | |
} | |
if (keys.hasNext()) { | |
builder.append(","); | |
} | |
} | |
builder | |
.append("\n") | |
.append(tabs(level)) | |
.append("}"); | |
} | |
} | |
public static void formatJson(StringBuilder builder, int level, JSONArray jsonArray) throws JSONException { | |
if (jsonArray != null && builder != null) { | |
int len = jsonArray.length(); | |
builder.append(" ["); | |
for (int i = 0; i < len; i++) { | |
builder.append("\n"); | |
Object o = jsonArray.get(i); | |
if (o instanceof JSONObject) { | |
formatJson(builder, level + 1, (JSONObject) o); | |
} else if (o instanceof String) { | |
builder | |
.append(tabs(level + 1)) | |
.append("\"") | |
.append(o) | |
.append("\""); | |
} else { | |
builder | |
.append(tabs(level + 1)) | |
.append(o); | |
} | |
if (len - i != 1) { | |
builder.append(","); | |
} | |
} | |
builder | |
.append("\n") | |
.append(tabs(level)) | |
.append("]"); | |
} | |
} | |
private static String tabs(int level) { | |
String tabs = ""; | |
for (int i = 0; i < level; i++) { | |
tabs += "\t"; | |
} | |
return tabs; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
not working this