首页 > 解决方案 > 改造 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) {

                }
            });

标签: androidjsonretrofitretrofit2

解决方案


您的登录响应类与您提供的 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;

}

推荐阅读