Skip to content
Advertisement

Loop Object from a single Array

im sending a Json Array from Activity A to Activity B, and i want to populate that Json Array into Spinner in Activity B. but when i Log the json array from Activity A, the data becomes a single object “NVAKSINATOR” :

[{“NVAKSINATOR”:”[{“NVAKSINATOR”:”20800″},{“NVAKSINATOR”:”0″},{“NVAKSINATOR”:”77350″},{“NVAKSINATOR”:”51750″},{“NVAKSINATOR”:”30000″},{“NVAKSINATOR”:”51500″},{“NVAKSINATOR”:”25750″},{“NVAKSINATOR”:”30900″}]”}]

i want that number like 20800 , 0 , 77350 to be populated in spinner but i dont know how to loop in single object like that. please help

Advertisement

Answer

SOLVED!

what im doing is, first get the first of index and get the name of my object :

JsonArray.getJsonObject(0).getString("NVAKSINATOR");

after that, you’ll get :

[{"NVAKSINATOR":"20800"},
 {"NVAKSINATOR":"0"},
 {"NVAKSINATOR":"77350"},
 {"NVAKSINATOR":"51750"},
 {"NVAKSINATOR":"30000"}, 
 {"NVAKSINATOR":"51500"},
 {"NVAKSINATOR":"25750"},
 {"NVAKSINATOR":"30900"}]

last step this is the easiest part, what i do is just looping that array. coz now the length is not 1.

for (int i = 0; i < obj.length(); i++) {  
    JSONObject jo = new JSONObject();  
    jo = obj.getJSONObject(i); 
    listvaksinator.add(jo.getString("NVAKSINATOR")); 
}

and now i got the number inside NVAKSINATOR 🙂

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement