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

3
  • Possible duplicate of How to parse JSON in Android Commented Aug 17, 2016 at 19:45
  • i have updated my question now can anyone tell me how to parse and save in arraylist Commented Aug 17, 2016 at 19:52
  • Updated answer to show how you can parse through the JSON array and save it to an array list Commented Aug 17, 2016 at 20:04

2 Answers 2

1
JSONArray jsonArray = new JSONArray(mainJSON.getJSONArray("participants"))

Then you just use your object like this:

 jsonArray.getJSONObject(0)
Sign up to request clarification or add additional context in comments.

1 Comment

i am getting an error jsonexception: not a primitive array
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.
}

6 Comments

if there is two values inside participants like [abc, xyz] then how to parse and save in arraylist
Value Socrates at 0 of type java.lang.String cannot be converted to JSONObject
Switch participants.getString(i) to participants.getJSONObject(i)
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.
Attempt to invoke interface method 'boolean java.util.List.add(java.lang.Object)' on a null object reference.. I am using List<String> to save the data inside participants and i am getting this error.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.