首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

如何获取JSONArray的最深层元素

获取JSONArray的最深层元素可以通过递归的方式来实现。下面是一个示例代码:

代码语言:txt
复制
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class Main {
    public static void main(String[] args) {
        String jsonString = "[{\"name\":\"John\",\"age\":30,\"city\":\"New York\"},[1,2,3,[4,5,6,[7,8,9]]],{\"key\":\"value\"}]";
        try {
            JSONArray jsonArray = new JSONArray(jsonString);
            JSONObject deepestElement = getDeepestElement(jsonArray);
            System.out.println("最深层元素:" + deepestElement.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public static JSONObject getDeepestElement(JSONArray jsonArray) throws JSONException {
        JSONObject deepestElement = null;
        int maxDepth = -1;

        for (int i = 0; i < jsonArray.length(); i++) {
            Object element = jsonArray.get(i);
            if (element instanceof JSONArray) {
                JSONObject subElement = getDeepestElement((JSONArray) element);
                int depth = getDepth(subElement);
                if (depth > maxDepth) {
                    maxDepth = depth;
                    deepestElement = subElement;
                }
            } else if (element instanceof JSONObject) {
                int depth = getDepth((JSONObject) element);
                if (depth > maxDepth) {
                    maxDepth = depth;
                    deepestElement = (JSONObject) element;
                }
            }
        }

        return deepestElement;
    }

    public static int getDepth(JSONObject jsonObject) {
        int maxDepth = 0;

        String[] keys = JSONObject.getNames(jsonObject);
        if (keys != null) {
            for (String key : keys) {
                Object value = jsonObject.get(key);
                if (value instanceof JSONObject) {
                    int depth = getDepth((JSONObject) value) + 1;
                    if (depth > maxDepth) {
                        maxDepth = depth;
                    }
                }
            }
        }

        return maxDepth;
    }
}

这段代码首先定义了一个JSON字符串,然后通过JSONArray和JSONObject类来解析JSON字符串。getDeepestElement方法使用递归的方式遍历JSONArray中的元素,如果遇到嵌套的JSONArray或JSONObject,则继续递归调用getDeepestElement方法。getDepth方法用于计算JSONObject的深度。

该代码的输出结果是最深层元素的JSONObject对象。你可以根据需要对其进行进一步处理或获取其属性值。

腾讯云相关产品和产品介绍链接地址:

请注意,以上仅为示例产品,腾讯云还提供更多丰富的云计算产品和服务,可根据具体需求选择合适的产品。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

  • JSON與ajax使用方法

    是存储和交换文本信息的语法。类似 XML。 比 XML 更小、更快,更易解析。 JSON 是一种数据格式。它本身是一串字符串,只是它有固定格式的字符串,符合这个数据格式要求的字符串,我们称之为JSON。 JSON 常用来数据传输,因为它易于程序之前读写操作。 JSON 它其实是来自JavaScript对对象(Object)的定义。但是它作为数据格式来使用的时候,和JavaScript没有任何关系,它只是参照了JavaScript对对象定义的数据格式。 JSON 它可以服务任何语言,C、C++、Java、Objective-C、Python、Go、等,在各个语言中的字典、Map和JSON是类似的结构,所以它们之间可以相互转换。 JSON键值对数据结构如上图,以 “{” 开始,以 “}” 结束。中间包裹的为Key : Value的数据结构。

    02

    杨老师课堂之零基础学习JSON知识点

    演示代码: /** * 演示JSON-LIB小工具  */ public class Demo1 { /* * 当map来用 */ @Test public void fun1() { JSONObject map = new JSONObject(); map.put("name", "zhangSan"); map.put("age", 23); map.put("sex", "male"); String s = map.toString(); System.out.println(s); } /* * 当你已经有一个Person对象时,可以把Person转换成JSONObject对象 */ @Test public void fun2() { Person p = new Person("liSi", 32, "female"); // 把对象转换成JSONObject类型 JSONObject map = JSONObject.fromObject(p); System.out.println(map.toString()); } /** * JSONArray */ @Test public void fun3() { Person p1 = new Person("zhangSan", 23, "male"); Person p2 = new Person("liSi", 32, "female"); JSONArray list = new JSONArray(); list.add(p1); list.add(p2); System.out.println(list.toString()); } /** * 原来就有一个List,我们需要把List转换成JSONArray */ @Test public void fun4() { Person p1 = new Person("zhangSan", 23, "male"); Person p2 = new Person("liSi", 32, "female"); List<Person> list = new ArrayList<Person>(); list.add(p1); list.add(p2); System.out.println(JSONArray.fromObject(list).toString()); } }

    03
    领券