James James - 6 months ago 23
Android Question

List of JSON items in Android Studio

I have this json:

{
"sid": "BiQo7DA4lMoRkeGN8mdfBXackyBarCSSauQtNRRKOmcfo2Ah0XCjaI1yevEoxWa09TkTOYrwGixRMvBr15h1d2",
"submissions": [{
"submission_id": "1104764"
}, {
"submission_id": "1104765"
}]
}


How can I in AndroidStudio get a list of items by submission_id?(there is always 30 items in "submissions")

Thanks

UPDATED




I'm trying with this code but show exception.

for (int i = 0; i < jObject.length(); i++) {
JSONObject subm = jObject.getJSONObject("submissions");
JSONObject jObj = subm.getJSONObject(String.valueOf(i));
testdata = testdata + " " + jObject.getString("submission_id");
//
}


I basically want to get all the elements inside the "submissions"...
enter image description here

Answer

Try this one:

if (!jObject.isNull("submissions")) {
    JSONArray submissions = jObject.getJSONArray("submissions");

    for(int i = 0; i < submissions.length(); i++) {
        JSONObject submission = submissions.getJSONObject(i);
        String submissionId = submission.getString("submission_id");
    }
}