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

Android Studio中Java用Gson反序列化JSON后值全为Null的问题

解决Gson反序列化后所有值为Null的问题

问题根源

  1. JSON结构不匹配:返回的JSON最外层是包含getInformationResult键的对象,你直接反序列化到getInformationResult类,Gson无法匹配外层结构,导致所有字段赋值失败。
  2. 字段类型不匹配:JSON中的布尔值(如BE_Active)、数字(如BE_Owner_ID)被定义为String类型,Gson无法自动转换这些类型,会跳过赋值。
  3. 字段名不匹配:实体类中的Currency字段,JSON里对应的是Currency_ID,无映射关系导致赋值失败。
  4. 数组字段定义错误:JSON中的Errors是数组类型,但你定义成了单个Errors对象,无法解析。
  5. 缺少无参构造函数:Gson默认需要无参构造函数实例化对象,你的实体类只有带参构造,无法正常创建实例。

修复步骤

1. 创建外层包裹类

匹配JSON的外层结构:

public class ApiResponse {
    @SerializedName("getInformationResult")
    public GetInformationResult getInformationResult;
}

注:Java类名遵循大驼峰规范,将原getInformationResult改为GetInformationResult

2. 修正实体类字段

调整类型、字段名映射,并添加无参构造:

import com.google.gson.annotations.SerializedName;
import java.util.List;

public class GetInformationResult {
    @SerializedName("Errors")
    public List<Error> errors; // 改为List匹配JSON数组
    @SerializedName("User_Name")
    public String userName;
    @SerializedName("BE_Active")
    public boolean beActive; // 布尔类型匹配JSON的false
    @SerializedName("BE_Owner_ID")
    public int beOwnerId; // 数字类型匹配JSON的0
    @SerializedName("BT_ID")
    public int btId;
    @SerializedName("CLC_Sale")
    public int clcSale;
    @SerializedName("CLC_Shared_Balance")
    public boolean clcSharedBalance;
    @SerializedName("Cash")
    public boolean cash;
    @SerializedName("Client_Active")
    public boolean clientActive;
    @SerializedName("Client_Alias")
    public String clientAlias;
    @SerializedName("Client_ID")
    public int clientId;
    @SerializedName("Clients_Balance_By_Brand")
    public Object clientsBalanceByBrand;
    @SerializedName("Contact_Active")
    public boolean contactActive;
    @SerializedName("Contact_ID")
    public int contactId;
    @SerializedName("Credit")
    public boolean credit;
    @SerializedName("Currency_ID") // 修正字段名匹配JSON
    public int currencyId;
    @SerializedName("Error_Codes")
    public Object errorCodes;
    @SerializedName("Father_ID")
    public int fatherId;
    @SerializedName("Labels")
    public Object labels;
    @SerializedName("Language_ID")
    public int languageId;
    @SerializedName("Parent_ID")
    public int parentId;
    @SerializedName("Schedule_Time_End")
    public String scheduleTimeEnd;
    @SerializedName("Schedule_Time_Start")
    public String scheduleTimeStart;
    @SerializedName("Server_Date_Time")
    public String serverDateTime;
    @SerializedName("Server_TZ_ISO_Notation")
    public String serverTzIsoNotation;
    @SerializedName("TZ_ISO_Notation")
    public String tzIsoNotation;
    @SerializedName("TT_ID")
    public int ttId;
    @SerializedName("Template_ID")
    public int templateId;
    @SerializedName("User_Active")
    public boolean userActive;
    @SerializedName("User_Email")
    public String userEmail;

    // 必须添加无参构造函数
    public GetInformationResult() {}

    // 保留带参构造(可选)
    public GetInformationResult(List<Error> errors, String userName, boolean beActive, int beOwnerId, int btId, int clcSale, boolean clcSharedBalance, boolean cash, boolean clientActive, String clientAlias, int clientId, Object clientsBalanceByBrand, boolean contactActive, int contactId, boolean credit, int currencyId, Object errorCodes, int fatherId, Object labels, int languageId, int parentId, String scheduleTimeEnd, String scheduleTimeStart, String serverDateTime, String serverTzIsoNotation, String tzIsoNotation, int ttId, int templateId, boolean userActive, String userEmail) {
        this.errors = errors;
        this.userName = userName;
        this.beActive = beActive;
        this.beOwnerId = beOwnerId;
        this.btId = btId;
        this.clcSale = clcSale;
        this.clcSharedBalance = clcSharedBalance;
        this.cash = cash;
        this.clientActive = clientActive;
        this.clientAlias = clientAlias;
        this.clientId = clientId;
        this.clientsBalanceByBrand = clientsBalanceByBrand;
        this.contactActive = contactActive;
        this.contactId = contactId;
        this.credit = credit;
        this.currencyId = currencyId;
        this.errorCodes = errorCodes;
        this.fatherId = fatherId;
        this.labels = labels;
        this.languageId = languageId;
        this.parentId = parentId;
        this.scheduleTimeEnd = scheduleTimeEnd;
        this.scheduleTimeStart = scheduleTimeStart;
        this.serverDateTime = serverDateTime;
        this.serverTzIsoNotation = serverTzIsoNotation;
        this.tzIsoNotation = tzIsoNotation;
        this.ttId = ttId;
        this.templateId = templateId;
        this.userActive = userActive;
        this.userEmail = userEmail;
    }

    @Override
    public String toString() {
        return new com.google.gson.Gson().toJson(this);
    }
}

3. 定义Error实体类

匹配JSON中的错误数组元素:

import com.google.gson.annotations.SerializedName;

public class Error {
    @SerializedName("Error_Code")
    public String errorCode;
    @SerializedName("Error_Description")
    public String errorDescription;
    @SerializedName("Error_ID")
    public int errorId;
    @SerializedName("Error_Name")
    public String errorName;

    public Error() {}
}

4. 修改反序列化代码

先反序列化到外层类,再获取目标对象:

Gson gson = new Gson();
ApiResponse apiResponse = gson.fromJson(response, ApiResponse.class);
GetInformationResult respuesta = apiResponse.getInformationResult;

额外建议

  • 遵循Java命名规范:类名大驼峰,字段名小驼峰,搭配@SerializedName映射JSON蛇形命名,提升可读性。
  • 不确定字段类型时,可先定义为Object,或使用@Nullable注解处理null值。
  • 使用GsonFormat插件自动生成实体类,避免手动编写的类型/字段名错误。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.06 13:54:56