简体   繁体   English

改造多个 JSON 响应

[英]Retrofit multiple JSON Response

Why can't I assign a value to status during JSON response.为什么我不能在 JSON 响应期间为状态分配值。 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因为状态在名为 data 的对象内,所以你应该创建数据类来保存状态,例如

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 :正如 Marwa Eltayeb 所回答的那样,但我们可以通过以下方式进一步修改答案:

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;
        }

    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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