You need to enable JavaScript to run this app. 如何用Java从JSON字符串提取数组内hierarchy_name字段值
优惠活动
大模型
产品
解决方案
定价
更多

如何用Java从JSON字符串提取数组内hierarchy_name字段值

从指定嵌套JSON结构提取hierarchy_name字段的Java实现

给定的JSON是典型的Elasticsearch搜索响应结构,目标提取路径为根对象 → hits节点 → hits数组 → 数组单个元素 → _source子节点 → hierarchy_name字段,下面给出3种Java生态最常用的实现方案,所有代码都做了空值兼容,不会因为字段缺失直接抛出空指针异常。

方案1:使用Jackson(Spring生态默认内置的JSON库)

无需提前定义实体类,适合快速解析、字段结构不固定的场景

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.ArrayList;
import java.util.List;

public class JacksonExtractDemo {
    public static List<String> extractHierarchyNames(String jsonStr) throws Exception {
        List<String> resultList = new ArrayList<>();
        ObjectMapper objectMapper = new ObjectMapper();
        // 将JSON字符串解析为树结构节点
        JsonNode rootNode = objectMapper.readTree(jsonStr);
        // 逐层定位到hits下的hits数组,path方法在路径不存在时返回空节点,不会触发空指针
        JsonNode hitsArrayNode = rootNode.path("hits").path("hits");
        if (!hitsArrayNode.isArray()) {
            return resultList;
        }
        // 遍历数组的每个命中对象
        for (JsonNode hitItem : hitsArrayNode) {
            // 从每个命中对象中定位_source下的hierarchy_name字段
            JsonNode hierarchyNode = hitItem.path("_source").path("hierarchy_name");
            if (!hierarchyNode.isNull()) {
                resultList.add(hierarchyNode.asText());
            }
        }
        return resultList;
    }
}

方案2:使用Fastjson2(国内项目常用的高性能JSON库)

解析速度快,API调用简便,适合高并发业务场景

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import java.util.ArrayList;
import java.util.List;

public class FastjsonExtractDemo {
    public static List<String> extractHierarchyNames(String jsonStr) {
        List<String> resultList = new ArrayList<>();
        // 解析JSON字符串为根对象
        JSONObject rootObj = JSON.parseObject(jsonStr);
        // 逐层取值,空值短路返回
        JSONObject hitsObj = rootObj.getJSONObject("hits");
        if (hitsObj == null) return resultList;
        JSONArray hitsArray = hitsObj.getJSONArray("hits");
        if (hitsArray == null) return resultList;
        // 遍历数组元素
        for (int i = 0; i < hitsArray.size(); i++) {
            JSONObject hitItem = hitsArray.getJSONObject(i);
            if (hitItem == null) continue;
            JSONObject sourceObj = hitItem.getJSONObject("_source");
            if (sourceObj == null) continue;
            String hierarchyName = sourceObj.getString("hierarchy_name");
            if (hierarchyName != null) {
                resultList.add(hierarchyName);
            }
        }
        return resultList;
    }
}

方案3:使用原生org.json实现(无第三方重型依赖场景)

适合不想引入大型JSON框架、对依赖体积敏感的项目

import org.json.JSONArray;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;

public class OrgJsonExtractDemo {
    public static List<String> extractHierarchyNames(String jsonStr) {
        List<String> resultList = new ArrayList<>();
        JSONObject rootObj = new JSONObject(jsonStr);
        // 逐层判断字段是否存在,避免取不存在的key报错
        if (!rootObj.has("hits")) return resultList;
        JSONObject hitsObj = rootObj.getJSONObject("hits");
        if (!hitsObj.has("hits")) return resultList;
        JSONArray hitsArray = hitsObj.getJSONArray("hits");
        // 遍历数组元素
        for (int i = 0; i < hitsArray.length(); i++) {
            JSONObject hitItem = hitsArray.getJSONObject(i);
            if (hitItem.has("_source")) {
                JSONObject sourceObj = hitItem.getJSONObject("_source");
                if (sourceObj.has("hierarchy_name")) {
                    resultList.add(sourceObj.getString("hierarchy_name"));
                }
            }
        }
        return resultList;
    }
}

效果验证

使用提供的示例JSON执行上述任意方法,最终得到的提取结果为两个值为PRODUCT的字符串,和示例结构里的字段值完全匹配。


内容的提问来源于stack exchange,提问作者user13593864

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.23 02:45:04