Skip to content

Instantly share code, notes, and snippets.

@64lines
Last active August 5, 2016 04:55
Show Gist options
  • Select an option

  • Save 64lines/b2130eb365d9c319ec0a505c36dcf764 to your computer and use it in GitHub Desktop.

Select an option

Save 64lines/b2130eb365d9c319ec0a505c36dcf764 to your computer and use it in GitHub Desktop.
Get JSON Data to put it into an Object
public List<Comment> obtainList(String jsonResponse) {
List<Comment> commentList = new ArrayList<>();
try {
JSONArray jsonArray = new JSONArray(jsonResponse);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
Comment comment = new Comment();
comment.setId(jsonObject.get("pk").toString());
comment.setScore(jsonObject.getJSONObject("fields").get("score").toString());
comment.setUserId(jsonObject.getJSONObject("fields").get("user").toString());
comment.setComment(jsonObject.getJSONObject("fields").get("comment").toString());
comment.setConferenceId(jsonObject.getJSONObject("fields").get("conference").toString());
commentList.add(comment);
}
} catch (JSONException e) {
e.printStackTrace();
}
return commentList;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment