Skip to content

Instantly share code, notes, and snippets.

@zxkane
Created March 14, 2012 16:00
Show Gist options
  • Save zxkane/2037476 to your computer and use it in GitHub Desktop.
Save zxkane/2037476 to your computer and use it in GitHub Desktop.
dump a string that presents json data
public static void main(String[] args) throws JSONException {
JSONObject obj = new JSONObject(
"{}"); //$NON-NLS-1$
dump(0, "", obj);
}
private static void dump(int level, String name, Object obj) throws JSONException {
for (int i = 0; i < level; i++)
System.out.print("\t");
if (obj instanceof JSONObject) {
System.out.println("JSONObject: " + name);
JSONObject jobj = (JSONObject) obj;
for (Iterator iter = jobj.keys(); iter.hasNext();) {
String key = (String) iter.next();
Object child = jobj.get(key);
dump(level + 1, key, child);
}
} else if (obj instanceof JSONArray) {
System.out.println("JSONArray: " + name);
JSONArray jarray = (JSONArray) obj;
for (int i = 0; i < jarray.length(); i++) {
dump(level + 1, "array item " + i, jarray.get(i));
}
} else {
System.out.println(name + ": " + obj);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment