0

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.

Romeo Sierra
  • 1,666
  • 1
  • 17
  • 35
Red Magda
  • 398
  • 4
  • 17

3 Answers3

1

The only problem I can see here is the single quotes that are enclosing the JSON string. Other than that, the code works flawlessly in Intellij IDEA 2018.1.3.

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");
    }
    System.out.println(Arrays.toString(servers)); //Added for testing purposes
    System.out.println(Arrays.toString(itemsId)); //Added for testing purposes
} catch (JSONException e) {
    e.printStackTrace();
} catch(Exception e) {
    e.printStackTrace();
}

The two System.out.println(Arrays.toString(arr)) calls gives the expected output. No problems.

Romeo Sierra
  • 1,666
  • 1
  • 17
  • 35
0

I Executed the following code which is a replica of yours (changed the jsonString).

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");
                System.out.println(servers[i]);
                itemsId[i] = o.getInt("srvid");
                System.out.println(itemsId[i]);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        } catch(Exception e) {
            e.printStackTrace();
        }

And got the response.

Label 1
1
label 2
2

So code looks good, if there is nothing wrong with JSON String.

Hearen
  • 7,420
  • 4
  • 53
  • 63
sandeshch
  • 11
  • 5
0

So what I did is I have initialized the length of the arrays by the size of the JSONArray. And taken them inside the try catch.

try {
    JSONArray array = new JSONArray(jsonString);
    String[] servers = new String[array.length()];
    Integer[] itemsId = new Integer[array.length()];
    for (int i = 0; i < array.length(); i++) {
        JSONObject o = array.getJSONObject(i);
        servers[i] = o.getString("label");
        itemsId[i] = o.getInt("srvid");
    }

    //REST OF THE CODES
} catch (JSONException e) {
    e.printStackTrace();
} catch(Exception e) {
    e.printStackTrace();
}
Red Magda
  • 398
  • 4
  • 17