Guest User

Untitled

a guest
Nov 21st, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. private ArrayList<HashMap<String, String>> ParseJSON(String json) {
  2. if (json != null) {
  3. try {
  4. // Hashmap for ListView
  5. ArrayList<HashMap<String, String>> studentList = new ArrayList<HashMap<String, String>>();
  6.  
  7. JSONObject jsonObj = new JSONObject(json);
  8.  
  9. // Getting JSON Array node
  10. JSONArray students = jsonObj.getJSONArray(TAG_STUDENTINFO);
  11.  
  12. // looping through All Students
  13. for (int i = 0; i < students.length(); i++) {
  14. JSONObject c = students.getJSONObject(i);
  15.  
  16. String id = c.getString(TAG_ID);
  17. String name = c.getString(TAG_NAME);
  18. String email = c.getString(TAG_EMAIL);
  19. String address = c.getString(TAG_ADDRESS);
  20. String gender = c.getString(TAG_GENDER);
  21.  
  22. // Phone node is JSON Object
  23. JSONObject phone = c.getJSONObject(TAG_PHONE);
  24. String mobile = phone.getString(TAG_PHONE_MOBILE);
  25. String home = phone.getString(TAG_PHONE_HOME);
  26.  
  27. // tmp hashmap for single student
  28. HashMap<String, String> student = new HashMap<String, String>();
  29.  
  30. // adding each child node to HashMap key => value
  31. student.put(TAG_ID, id);
  32. student.put(TAG_NAME, name);
  33. student.put(TAG_EMAIL, email);
  34. student.put(TAG_PHONE_MOBILE, mobile);
  35.  
  36. // adding student to students list
  37. studentList.add(student);
  38. }
  39. return studentList;
  40. } catch (JSONException e) {
  41. e.printStackTrace();
  42. return null;
  43. }
  44. } else {
  45. Log.e("ServiceHandler", "Couldn't get any data from the url");
  46. return null;
  47. }
  48. }
Add Comment
Please, Sign In to add comment