0
[
  {
    "subject": "Top exotic beaches in Greece",
    "participants": [
      "Aristotle", "Socrates"
    ],
    "preview": "Plato,\nThis is what I was talking about for the bachelor party!",
    "isRead": false,
    "isStarred": true,
    "ts": 1471451322,
    "id": 5
  }
]

How to parse "participants" from this json... thanx in advance

  • Possible duplicate of [How to parse JSON in Android](http://stackoverflow.com/questions/9605913/how-to-parse-json-in-android) – mic4ael Aug 17 '16 at 19:45
  • i have updated my question now can anyone tell me how to parse and save in arraylist –  Aug 17 '16 at 19:52
  • Updated answer to show how you can parse through the JSON array and save it to an array list – Alvin Abia Aug 17 '16 at 20:04

2 Answers2

1
try {
    JSONArray participants = jsonObject.getJSONArray("participants")
    for (int i = 0; i < participants.length(); i++) {
        yourArrayList.add(participants.getString(i))
    }
} catch(JSONException e) {
    // Handle exception.
}
Alvin Abia
  • 1,221
  • 1
  • 17
  • 18
  • if there is two values inside participants like [abc, xyz] then how to parse and save in arraylist –  Aug 17 '16 at 19:50
  • Value Socrates at 0 of type java.lang.String cannot be converted to JSONObject –  Aug 17 '16 at 20:05
  • Switch participants.getString(i) to participants.getJSONObject(i) – Alvin Abia Aug 17 '16 at 20:07
  • If the contents of your participants array are strings, you use getString(i), and getJSONObject(i) if they're objects. Your compiler is telling you that they're strings but you're trying to access them as JSON objects. – Alvin Abia Aug 17 '16 at 20:12
  • Attempt to invoke interface method 'boolean java.util.List.add(java.lang.Object)' on a null object reference.. I am using List to save the data inside participants and i am getting this error. –  Aug 17 '16 at 20:19
  • Make sure you initialize the array list before adding things to it: List yourArrayList = new ArrayList<>; – Alvin Abia Aug 17 '16 at 20:21
1
JSONArray jsonArray = new JSONArray(mainJSON.getJSONArray("participants"))

Then you just use your object like this:

 jsonArray.getJSONObject(0)
vrfloppa
  • 374
  • 4
  • 14