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

Gson解析含有效值的JSON时返回null的问题求助

问题描述

使用Gson解析JSON数据时,解析后的Example对象直接为null,而非仅quakeml字段为空。相关数据与代码如下:

待解析JSON数据

{
  "quakeml": {
    "eventParameters": {
        "event": [
              {
          "description": [
            {
              "text": "S0959a",
              "type": "earthquake name"
            }
          ],
          "origin": {
            "longitude": {
              "value": 135.6234
            },
            "latitude": {
              "value": 4.5024
            },
            "distance": {
              "value": 32.5
            }
          },
          "magnitude": [
            {
              "mag": {
                "value": 3.2
              },
              "type": "MbS",
              "originID": "smi:insight.mqs/origin/20220623-123048/ebd6d417"
            },
            {
              "mag": {
                "value": 3.2
              },
              "type": "MbP",
              "originID": "smi:insight.mqs/origin/20220623-123048/ebd6d417"
            },
            {
              "mag": {
                "value": 3,
                "lowerUncertainty": 0.4,
                "upperUncertainty": 0.4
              },
              "type": "MWspec",
              "originID": "smi:insight.mqs/origin/20220623-123048/ebd6d417"
            },
            {
              "mag": {
                "value": 3,
                "lowerUncertainty": 0.4,
                "upperUncertainty": 0.4
              },
              "type": "MW",
              "originID": "smi:insight.mqs/origin/20220623-123048/ebd6d417"
            }
          ]
        },
        {
          "description": [
            {
              "text": "S0959a",
              "type": "earthquake name"
            }
          ],
          "origin": {
            "longitude": {
              "value": 135.6234
            },
            "latitude": {
              "value": 4.5024
            },
            "distance": {
              "value": 32.5
            }
          },
          "magnitude": [
            {
              "mag": {
                "value": 3.2
              },
              "type": "MbS",
              "originID": "smi:insight.mqs/origin/20220623-123048/ebd6d417"
            },
            {
              "mag": {
                "value": 3.2
              },
              "type": "MbP",
              "originID": "smi:insight.mqs/origin/20220623-123048/ebd6d417"
            },
            {
              "mag": {
                "value": 3,
                "lowerUncertainty": 0.4,
                "upperUncertainty": 0.4
              },
              "type": "MWspec",
              "originID": "smi:insight.mqs/origin/20220623-123048/ebd6d417"
            },
            {
              "mag": {
                "value": 3,
                "lowerUncertainty": 0.4,
                "upperUncertainty": 0.4
              },
              "type": "MW",
              "originID": "smi:insight.mqs/origin/20220623-123048/ebd6d417"
            }
          ]
        }
      ]
    }
  }
}

注:原JSON末尾存在多余的``符号,属于语法错误,会直接导致解析失败。

Gson调用代码

Example example=null;
example = gson.fromJson(content, Example.class);

生成的Example类

import javax.annotation.Generated;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

@Generated("jsonschema2pojo")
public class Example {

    @SerializedName("quakeml")
    @Expose
    private Quakeml quakeml;

    public Quakeml getQuakeml() {
        return quakeml;
    }

    public void setQuakeml(Quakeml quakeml) {
        this.quakeml = quakeml;
    }

}

调试时确认example变量本身为null,并非quakeml字段为空。

排查与解决方案

1. 修复JSON语法错误

优先删除原JSON末尾多余的``符号,使用JSON校验工具确认格式合法——Gson解析无效JSON时会直接返回null,这是本次问题的核心诱因。

2. 校验输入的content变量

解析前确认content字符串不为空、不为null,且是完整的目标JSON内容。可通过打印或断点查看content实际值,避免传入空字符串、截断的JSON或非JSON格式内容。

3. 检查Gson实例配置

若使用GsonBuilder自定义Gson实例,排查以下配置:

  • 若启用excludeFieldsWithoutExposeAnnotation(),需确保所有嵌套POJO类(如Quakeml、EventParameters等)的字段都带有@Expose注解;
  • 避免添加与当前JSON格式冲突的规则(如不匹配的日期适配器、类型转换器等)。

4. 确认POJO类完整性

jsonschema2pojo生成的POJO需包含所有嵌套层级的类(如Quakeml、Event、Origin等),且类中字段的@SerializedName注解需与JSON键名完全匹配。缺少嵌套类或字段名不匹配会导致解析失败,极端情况返回null。

5. 捕获解析异常定位问题

在解析代码中添加异常捕获,获取具体错误信息:

Example example = null;
try {
    example = gson.fromJson(content, Example.class);
} catch (JsonSyntaxException e) {
    e.printStackTrace(); // 打印JSON语法错误详情
} catch (JsonIOException e) {
    e.printStackTrace(); // 处理IO类错误
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.22 14:46:07