I am trying to populate the server
and the itemsId
by iterating over a JSONArray
.
String jsonString = '"[{\"label\":\"Label 1\",\"srvid\":1},{\"label\":\"label 2\",\"srvid\":2}]"';
String[] servers = new String[100];
Integer itemsId[] = new Integer[100];
try {
JSONArray array = new JSONArray(jsonString);
for (int i = 0; i < array.length(); i++) {
JSONObject o = array.getJSONObject(i);
servers[i] = o.getString("label");
itemsId[i] = o.getInt("srvid");
}
} catch (JSONException e) {
e.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
}
However, I am getting ArrayIndexOutOfBoundsException
here but I don't know how to further solve this. I wonder what could be wrong in the declaration of the arrays and how they are populated.