java - 检查 JSONArray 是否实际上是 boolean 数组的整数

标签 java json

是否可以检查 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/

相关文章:

java - Flink Gelly 扩展边缘类并在 DataSet 中使用它

java - 迭代器和 For Each 循环的区别

java - Java中如何删除JSON多余的行?

c# - 使用JSON.NET序列化复杂字典时更改 'Key'和 'Value'的名称

java - 有没有更快的方法来使用java在eclipse中设置方法?

java - 我需要在 Quartz 调度程序关闭时执行任务

java - 将 JSON 转换为 CSV

java - 将 firebase 数据检索为 json

java - 如何获取对匿名对象的引用 2 级

javascript - 无法读取属性 api Rest