Skip to content

Instantly share code, notes, and snippets.

@smoak
Created February 17, 2012 19:40
Show Gist options
  • Save smoak/1855071 to your computer and use it in GitHub Desktop.
Save smoak/1855071 to your computer and use it in GitHub Desktop.
// assumes you have already gotten this json back from an Http server:
// [{"id":1,"firstName":"j","lastName":"v"},{"id":2,"firstName":"Dr. a","lastName":"b"}]
// and stored it in a String called json
JSONArray json = new JSONArray(json);
// loop over each individual json object:
// a single json object looks like: {"id":1,"firstName":"j","lastName":"v"}
int count = json.length();
for (int i = 0; i < count; i++) {
JSONObject jsonObj = json.getJSONObject(i);
// now we can access the individual keys
// in the jsonObj:
int id = jsonObj.getInt("id");
String firstName = jsonObj.getString("firstName");
String lastName = jsonObj.getString("lastName");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment