在后期改造中获取Json Array

时间:2019-06-09 18:07:57

标签: java android json retrofit

我想从帖子响应中检索数组时遇到问题

这是我的帖子

{
    "user_email": "xxxx@gmail.com",
    "user_password": "12345"
}

这是我的回复,我想获取令牌值

{
    "status": true,
    "code": 200,
    "message": "Request Succeded: Login success",
    "data": [
        {
            "token": "bsWIVXTLuud2ZbdnUvI8037fT7D0t7MTvusBrNjskah"
        }
    ]
}

这是我的模型LoginModel.java

@SerializedName("data")
@Expose
private Data data ;

public Data getData() {
    return data;
}

public void setData (Data data) {
    this.data = data;
}

public LoginModel(String user_email, String user_password) {
    this.user_email = user_email;
    this.user_password = user_password;
}

Data.java

public String getToken() {
    return token;
}

public void setToken(String token) {
   this.token = token;
}

有我之前尝试过的代码,文本视图什么也不显示

LoginModel loginModelresponse = response.body();

String token = loginModelresponse.getData().getToken();

textView.setText(token);

我想从我的帖子回复中获取令牌。

3 个答案:

答案 0 :(得分:1)

您的数据值是令牌数组,而不是对象本身

使用令牌字符串字段创建令牌类,然后像这样替换数据类

@SerializedName("data")
@Expose
private List<Token> data ;

要获取令牌,您必须迭代列表

答案 1 :(得分:0)

请尝试更改LoginModel类,因为您的响应数据对象是数组类型

public class LoginModel {
@SerializedName("status")
@Expose
private Boolean status;
@SerializedName("code")
@Expose
private Integer code;
@SerializedName("message")
@Expose
private String message;
@SerializedName("data")
@Expose
private List<Data> data = null;

public Boolean getStatus() {
return status;
}

public void setStatus(Boolean status) {
this.status = status;
}

public Integer getCode() {
return code;
}

 public void setCode(Integer code) {
 this.code = code;
 }

 public String getMessage() {
return message;
 }

public void setMessage(String message) {
this.message = message;
}

public List<Data> getData() {
return data;
}

 public void setData(List<Data> data) {
 this.data = data;
}

}

答案 2 :(得分:0)

使用this网站在Java中生成正确的响应类

 public class Tokens implements Serializable
    {

    @SerializedName("token")
    @Expose
    private String token;
    private final static long serialVersionUID = 1577013820593763604L;

    public String getToken() {
    return token;
    }

    public void setToken(String token) {
    this.token = token;
    }

    }
  //  -----------------------------------com.example.Response.java-----------

    package com.example;

    import java.io.Serializable;
    import java.util.List;
    import com.google.gson.annotations.Expose;
    import com.google.gson.annotations.SerializedName;

    public class Response implements Serializable
    {

    @SerializedName("status")
    @Expose
    private boolean status;
    @SerializedName("code")
    @Expose
    private long code;
    @SerializedName("message")
    @Expose
    private String message;
    @SerializedName("data")
    @Expose
    private List<Tokens> data = null;
    private final static long serialVersionUID = -3002290394951662690L;

    public boolean isStatus() {
    return status;
    }

    public void setStatus(boolean status) {
    this.status = status;
    }

    public long getCode() {
    return code;
    }

    public void setCode(long code) {
    this.code = code;
    }

    public String getMessage() {
    return message;
    }

    public void setMessage(String message) {
    this.message = message;
    }

    public List<Tokens> getData() {
    return data;
    }

    public void setData(List<Tokens> data) {
    this.data = data;
    }

    }