android - 改造 JSON GET 请求返回 null
问题描述
改造 JSON GET 请求为嵌套字段返回 null。如何访问 UserID 字段数据?我究竟做错了什么 ?我不知道如何使用改造来解析 json。熟悉使用 Retrofit 解析简单的 json,但不熟悉使用 Retrofit 解析嵌套的 Json。
JSON数据:
{
"Status": "Success",
"ErrorMessage": null,
"ErrorKey": null,
"RedirectUrl": null,
"Data": {
"UserDetails": {
"UserID": "d7a6397c-fa33-4268-a64a-b99d0e78dfcf"
}
}
}
获取数据接口:
@GET("Account/LoginAPi")
Call<LoginResponse> getUser(@Query("UserName") String username,
@Query("Password") String password);
POJO:
public class LoginResponse {
@SerializedName("UserName")
String UserName;
@SerializedName("Password")
String Password;
@SerializedName("UserID")
String UserID;
@SerializedName("Status")
String Status;
public LoginResponse(String UserName, String Password, String UserID, String Status)
{
this.UserName = UserName;
this.Password = Password;
this.UserID = UserID;
this.Status = Status;
}
public String getPassword() {
return Password;
}
public void setPassword(String Password){
this.Password = Password;
}
public String getUserName() {
return UserName;
}
public void setUserName(String UserName){
this.UserName = UserName;
}
public String getUserID() {
return UserID;
}
public void setUserID(String UserID) {
this.UserID = UserID;
}
public String getStatus() {
return Status;
}
public void setStatus(String Status) {
this.Status = Status;
}
}
主要活动:
GetData service = RetrofitClient.getRetrofitInstance().create(GetData.class);
Call<LoginResponse> call = service.getUser(username, password);
call.enqueue(new Callback<LoginResponse>() {
@Override
public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) {
pDialog.dismiss();
try {
String userid = response.body().getUserID();
toast(userid);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onFailure(Call<LoginResponse> call, Throwable t) {
}
});
解决方案
您的登录响应类与您提供的 json 不匹配,这是您的问题。
我通常使用jsonschema2pojo网站轻松解析 json 文件
将您的json粘贴到那里:
-------------- Data --------------------
public class Data {
@SerializedName("UserDetails")
public UserDetails userDetails;
}
-------------- LoginResponse ------------
package com.example;
import com.google.gson.annotations.SerializedName;
public class LoginResponse {
@SerializedName("Status")
public String status;
@SerializedName("ErrorMessage")
public Object errorMessage;
@SerializedName("ErrorKey")
public Object errorKey;
@SerializedName("RedirectUrl")
public Object redirectUrl;
@SerializedName("Data")
public Data data;
}
-----------------------------------com.example.UserDetails.java-----------------------------------
package com.example;
import com.google.gson.annotations.SerializedName;
public class UserDetails {
@SerializedName("UserID")
public String userID;
}
推荐阅读
- c++ - 编译器错误“class_name”不是类型,“class_name”没有命名类型 C++
- python - 将 TensorFlow 模型转换为 Pytorch 时出现大小不匹配错误
- javascript - .(dot) 在 javascript 中有什么作用?
- javascript - 为什么我的快速路由函数不会触发它们的回调函数?
- python - 当比较超过最大递归深度时,将递归函数转换为python中的迭代形式
- javascript - 使用 Cypress.io 从发送到 Google Number/Inbox 的消息中检索验证码
- python-3.x - 使用 API 自动将商店销售直接转化导入 Google Ads
- excel - 根据单元格值更改表格数据
- c++ - PCL八叉树std::bad_alloc c++
- yaml - Azure 管道参数格式化 YAML