You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java中如何解析JSON并仅提取summary length单个字段?

Java提取JSON中Summary下length字段方案

你当前生成的HereRoute类和提供的JSON结构不匹配:你给出的JSON根节点是Route对象组成的数组,没有外层包含routes字段的包装对象,直接用HereRoute反序列化会拿不到数据,可选择以下两种方案解析:


方案1:基于现有POJO类全量反序列化

适合后续可能需要用到其他字段的场景,操作步骤如下:

  1. 给所有POJO类补充私有字段对应的getter方法,否则Gson无法读取私有属性
  2. 直接将JSON反序列化为List<Route>类型,遍历提取字段:
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.List;

public class JsonParseDemo {
    public static void main(String[] args) {
        // 你的原始JSON字符串
        String jsonStr = "[{\"id\":\"f9952c24-1b44-4379-9aef-b10075e93562\",\"sections\":[{\"id\":\"7fe9f47e-9cfe-46c7-9c77-f729b9fb98a4\",\"type\":\"vehicle\",\"summary\":{\"duration\":23377,\"length\":480501,\"baseDuration\":22140}}]}]";
        
        Gson gson = new Gson();
        Type routeListType = new TypeToken<List<Route>>(){}.getType();
        List<Route> routeList = gson.fromJson(jsonStr, routeListType);
        
        // 遍历提取所有length字段
        for (Route route : routeList) {
            List<Section> sections = route.getSections();
            if (sections == null || sections.isEmpty()) continue;
            for (Section section : sections) {
                Summary summary = section.getSummary();
                if (summary != null && summary.getLength() != null) {
                    Integer length = summary.getLength();
                    // 此处写入你存入数据库的逻辑
                    System.out.println("提取到length:" + length);
                }
            }
        }
    }
}

方案2:轻量树解析(无需全量POJO)

适合只需要提取个别字段的场景,不需要维护一堆POJO类,直接通过JSON树结构捞取目标字段:

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

public class JsonParseDemo {
    public static void main(String[] args) {
        String jsonStr = "[{\"id\":\"f9952c24-1b44-4379-9aef-b10075e93562\",\"sections\":[{\"id\":\"7fe9f47e-9cfe-46c7-9c77-f729b9fb98a4\",\"type\":\"vehicle\",\"summary\":{\"duration\":23377,\"length\":480501,\"baseDuration\":22140}}]}]";
        
        Gson gson = new Gson();
        JsonArray rootArray = gson.fromJson(jsonStr, JsonArray.class);
        
        for (int i = 0; i < rootArray.size(); i++) {
            JsonObject routeObj = rootArray.get(i).getAsJsonObject();
            JsonArray sections = routeObj.getAsJsonArray("sections");
            if (sections == null || sections.isEmpty()) continue;
            for (int j = 0; j < sections.size(); j++) {
                JsonObject sectionObj = sections.get(j).getAsJsonObject();
                JsonObject summary = sectionObj.getAsJsonObject("summary");
                if (summary != null && summary.has("length") && !summary.get("length").isJsonNull()) {
                    Integer length = summary.get("length").getAsInt();
                    // 此处写入你存入数据库的逻辑
                    System.out.println("提取到length:" + length);
                }
            }
        }
    }
}

关于是否需要使用Gson这类JSON解析库

不需要手写JSON字符串解析逻辑,现有成熟解析库(Gson、Jackson、Fastjson都可)已经覆盖了各种边界场景,性能和稳定性都远高于自定义解析,优先选择你项目中已经引入的JSON解析库即可,避免额外引入依赖。

注意事项

  • 代码中已添加非空判断,避免JSON结构缺失对应字段时出现空指针异常
  • 如果你实际业务中的JSON外层确实存在包含routes字段的包装对象,再使用你生成的HereRoute类进行反序列化即可
  • 生产环境建议做数据格式校验,避免异常格式的JSON导致解析失败

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.25 15:45:04