简体   繁体   中英

Retrofit multiple JSON Response

Why can't I assign a value to status during JSON response. How to read the integer status? Thanks for help !

Model:
public class JsonResponse {
    public String code;
    public Integer status;

    public String getCode() { return code; }
    public Integer getStatus() { return status; }

}

Response JSON:
{
    "code": "jwt_auth_valid_token",
    "data": {
        "status": 200
    }
}

Result: getStatus(): Null

Because status is inside object called data so you should create data class that will save the status for example

public class Data {
    public int status;
}

public class JsonResponse {
    public String code;
    public Data data;

    public String getCode() { return code; }
    public Integer getStatus() { return data.status;}

}

as answered Marwa Eltayeb but we can modify the answer more with :

public class JsonResponse {
    @SerializedName("code")
    public String code;
    @SerializedName("data")
    public Data data;
    public String getCode() {
        return code;
    }

    public Data getData() {
        return data;
    }

    public class Data {

        @SerializedName("status")
        public int status;


        public int getStatus() {
            return status;
        }

    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM