是否可以检查 JSONArray 实际上是 boolean 数组还是整数数组。我已经在互联网上搜索了几个小时,但我找不到任何东西。谢谢。
最佳答案
您只需在第一个元素上使用 instanceof
即可:
JSONObject o = new JSONObject("{ \"a1\": [ false, true, false, true ], \"a2\": [1,2,3,4] }");
JSONArray a1 = o.getJSONArray("a1");
JSONArray a2 = o.getJSONArray("a2");
if (a1.length() > 0) {
if (a1.get(0) instanceof Boolean) {
System.out.println("a1 is Boolean array");
} else if (a1.get(0) instanceof Integer) {
System.out.println("a1 is Integer array");
} else {
System.out.println("a1 is some other type");
}
}
if (a2.length() > 0) {
if (a2.get(0) instanceof Boolean) {
System.out.println("a2 is Boolean array");
} else if (a2.get(0) instanceof Integer) {
System.out.println("a2 is Integer array");
} else {
System.out.println("a2 is some other type");
}
}
关于java - 检查 JSONArray 是否实际上是 boolean 数组的整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24388003/