Java - 尝试通过遍历 JSONArray 来填充数组时出现 ArrayIndexOutofBoundsException
Java - ArrayIndexOutofBoundsException when trying to populate arrays by iterating over a JSONArray
我正在尝试通过遍历 JSONArray
.
来填充 server
和 itemsId
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();
}
然而,我得到 ArrayIndexOutOfBoundsException
这里但我不知道如何进一步解决这个问题。我想知道数组的声明中可能有什么问题以及它们是如何填充的。
我执行了以下代码,它是您的副本(更改了 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();
}
并得到回复。
Label 1
1
label 2
2
所以代码看起来不错,如果 JSON 字符串没有问题的话。
我在这里看到的唯一问题是包含 JSON 字符串的单引号。除此之外,代码在 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();
}
两个 System.out.println(Arrays.toString(arr))
调用给出了预期的输出。没问题。
所以我所做的是通过 JSONArray 的大小初始化数组的长度。并将它们带入 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();
}
我正在尝试通过遍历 JSONArray
.
server
和 itemsId
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();
}
然而,我得到 ArrayIndexOutOfBoundsException
这里但我不知道如何进一步解决这个问题。我想知道数组的声明中可能有什么问题以及它们是如何填充的。
我执行了以下代码,它是您的副本(更改了 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();
}
并得到回复。
Label 1
1
label 2
2
所以代码看起来不错,如果 JSON 字符串没有问题的话。
我在这里看到的唯一问题是包含 JSON 字符串的单引号。除此之外,代码在 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();
}
两个 System.out.println(Arrays.toString(arr))
调用给出了预期的输出。没问题。
所以我所做的是通过 JSONArray 的大小初始化数组的长度。并将它们带入 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();
}