JSON 數據結構
{
"ErrorMessage": null,
"ResponseData": {
"Data": [{...}],
"Index": 1,
"PageSize": 1,
"TotalCount": 8
}
}
原始代碼
jsonObject = new JSONObject(responseInfo.result);
JsonsRootBean jsonsRootBean = gson.fromJson(responseInfo.result, JsonsRootBean.class);
Responsedata res = jsonsRootBean.getResponsedata();
System.out.println("--res-----" + res); // 輸出 NULL
排查步驟
- 先打印
responseInfo.result 確認原始 JSON 是否正確。
- 核對實體類
JsonsRootBean、Responsedata 字段名稱與 JSON 鍵是否完全一致(區分大小寫)。
- 測試發現:直接使用該 JSON 數據可正常解析,問題多在實體類定義不匹配。
關鍵點
- JSON 鍵
ResponseData 對應實體類字段需為 responseData 或加註解 @SerializedName("ResponseData")。
- 內部類
Data 列表泛型類型需明確。