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

仅关注JSON键时,使用Gson提取键的最优实现方法问询

使用Gson提取JSON对象键的推荐方案

给定如下JSON结构,我们只需要提取所有产品对应的键(如product_key_1),无需关心产品名称、价格等详情:

{
  "product_key_1": {
    "name": "product_name_1",
    "price": 10.99
  },
  "product_key_2": {
    "name": "product_name_2",
    "price": 24.99
  },
  "product_key_3": {
    "name": "product_name_3",
    "price": 7.49
  }
}

你提到的两种方法都可行,但从性能和语义明确性角度,更推荐第二种方案,以下是具体分析:

方法对比

1. Map<String, Object> 方式

Map<String, Object> products = gson.fromJson(rawJsonString, Object.class);
Set<String> productKeys = products.keySet(); 

这种方法确实能拿到键,但Gson会自动把每个value反序列化成对应的Java对象(比如内部的产品详情会被转成LinkedTreeMap),做了很多我们不需要的额外反序列化工作,在JSON体积较大时会浪费性能和内存。

2. Map<String, JsonElement> 方式

Map<String, JsonElement> products = gson.fromJson(rawJsonString, new TypeToken<Map<String, JsonElement>>() {}.getType());
Set<String> productKeys = products.keySet();

这种方法仅将JSON解析成Gson的抽象语法树节点(JsonElement),不会深入反序列化内部的产品详情,性能更优,同时代码语义更明确——我们只需要处理JSON的结构,不需要具体的业务对象。

更高效的进阶方案(适合大型JSON)

如果你的JSON体积很大,推荐直接用JsonReader流式读取,跳过不需要的value,避免构建整个Map对象,进一步节省内存:

Set<String> productKeys = new HashSet<>();
try (JsonReader reader = new JsonReader(new StringReader(rawJsonString))) {
    reader.beginObject();
    while (reader.hasNext()) {
        productKeys.add(reader.nextName());
        reader.skipValue(); // 直接跳过value,不做任何解析
    }
    reader.endObject();
} catch (IOException e) {
    // 根据业务需求处理异常
}

总结

  • 若JSON体积较小,Map<String, JsonElement>是简洁且够用的选择;
  • 若JSON体积较大,优先考虑JsonReader流式读取的方案,性能表现更优。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.13 16:55:16