如何取出 JSON 结构最后一层对象

在 Java 中处理 JSON 结构是一项常见的任务。JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,常用于前后端数据传输和存储。JSON 结构由键值对组成,可以是嵌套的,其中最后一层对象即为最终需要操作的数据。

本文将介绍一种方案,通过递归遍历 JSON 结构,取出最后一层对象。

问题描述

假设有一个 JSON 数据如下所示:

{
  "name": "John",
  "age": 30,
  "address": {
    "city": "New York",
    "zipcode": "12345"
  },
  "pets": [
    {
      "type": "dog",
      "name": "Max"
    },
    {
      "type": "cat",
      "name": "Lucy"
    }
  ]
}

我们想要取出最后一层的对象,即 { "type": "cat", "name": "Lucy" }

方案实现

1. 创建 JSON 数据

首先,我们需要创建一个 JSON 数据对象,用来模拟实际的数据。在 Java 中,可以使用 org.json 库来处理 JSON 数据。

import org.json.JSONObject;

public class JsonExample {

    public static void main(String[] args) {
        // 创建 JSON 数据
        JSONObject json = new JSONObject();
        json.put("name", "John");
        json.put("age", 30);
        
        JSONObject address = new JSONObject();
        address.put("city", "New York");
        address.put("zipcode", "12345");
        json.put("address", address);
        
        JSONObject pet1 = new JSONObject();
        pet1.put("type", "dog");
        pet1.put("name", "Max");
        
        JSONObject pet2 = new JSONObject();
        pet2.put("type", "cat");
        pet2.put("name", "Lucy");
        
        JSONArray pets = new JSONArray();
        pets.put(pet1);
        pets.put(pet2);
        
        json.put("pets", pets);
        
        System.out.println(json.toString());
    }
}

上述代码通过 JSONObjectJSONArray 创建了一个嵌套的 JSON 数据,并使用 toString 方法将其转换为字符串输出。

2. 递归遍历 JSON 结构

接下来,我们需要递归遍历 JSON 结构,找到最后一层的对象。我们可以定义一个递归函数来完成这个任务。

import org.json.JSONArray;
import org.json.JSONObject;

public class JsonExample {

    public static void main(String[] args) {
        // 创建 JSON 数据

        // ...

        // 递归遍历 JSON 结构
        traverseJson(json);
    }

    private static void traverseJson(Object obj) {
        if (obj instanceof JSONObject) {
            JSONObject jsonObject = (JSONObject) obj;
            for (String key : jsonObject.keySet()) {
                Object value = jsonObject.get(key);
                traverseJson(value);
            }
        } else if (obj instanceof JSONArray) {
            JSONArray jsonArray = (JSONArray) obj;
            for (int i = 0; i < jsonArray.length(); i++) {
                Object value = jsonArray.get(i);
                traverseJson(value);
            }
        } else {
            // 处理最后一层对象
            System.out.println(obj.toString());
        }
    }
}

上述代码中的 traverseJson 函数接受一个参数 obj,用来表示当前遍历到的 JSON 对象。如果 objJSONObject 类型,就对其进行遍历,如果 objJSONArray 类型,同样对其进行遍历。如果 obj 是最后一层的对象,即不再是 JSON 对象或数组,就进行相应的处理。

3. 取出最后一层对象

traverseJson 函数中,我们已经打印了最后一层对象。如果想要进一步操作该对象,可以将其存储到一个变量中。

import org.json.JSONArray;
import org.json.JSONObject;

public class JsonExample {

    private static JSONObject lastObject;

    public static void main(String[] args) {
        // 创建 JSON 数据

        // ...

        // 递归遍历 JSON 结构
        traverseJson(json);

        // 打印最后一层对象
        System.out.println(lastObject.toString());
    }

    private static void traverseJson(Object obj) {
        if (obj instanceof JSONObject) {
            JSONObject jsonObject = (JSONObject) obj;
            for (String key : jsonObject.keySet()) {
                Object value = jsonObject.get(key);
                traverseJson(value);