Created
March 14, 2012 16:00
-
-
Save zxkane/2037476 to your computer and use it in GitHub Desktop.
dump a string that presents json data
This file contains hidden or 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
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