Skip to content

Instantly share code, notes, and snippets.

@anttti
Last active December 17, 2015 09:48
Show Gist options
  • Save anttti/5589454 to your computer and use it in GitHub Desktop.
Save anttti/5589454 to your computer and use it in GitHub Desktop.
private String getPersonsAsJSON(List<Person> personList) throws JSONException {
JSONObject json = new JSONObject();
JSONArray jsonPeopleList = new JSONArray();
if (personList != null) {
for (Person p : personList) {
JSONObject personJson = new JSONObject();
personJson.put("firstname", p.getFirstName());
personJson.put("lastname", p.getLastName());
personJson.put("email", p.getEmail());
jsonPeopleList.put(personJson);
}
}
json.put("people", jsonPeopleList);
String jsonStr = json.toString();
Log.i(TAG, "Persons as JSON: " + jsonStr);
return jsonStr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment