我无法按照POJO类访问数据

时间:2018-11-22 05:29:16

标签: android getter-setter pojo

嗨,我收到以下json响应,我无法访问“ edit” jsonobject的数据,如何为此创建pojo?

示例

{
    "result":"",
    "responseMessage":"",
    "edit":{
        "id":"156",
        "user_id":"5466",

    },
    "data":[
         {
            "dataid":"1",
            "dataname":"tt"
        },
        {
            "dataid":"2",
            "dataname":"tt"
        }
    ]
}

如何为此响应创建pojo类

3 个答案:

答案 0 :(得分:1)

您的Json不正确。 编辑对象中还有一个额外的“ ”。删除它,然后尝试访问修改

的数据

如果您仍然无法访问,我会附上以下POJO供您使用。

public class ExPojo
{
    private String responseMessage;

    private String result;

    private Data[] data;

    private Edit edit;

    public String getResponseMessage ()
    {
        return responseMessage;
    }

    public void setResponseMessage (String responseMessage)
    {
        this.responseMessage = responseMessage;
    }

    public String getResult ()
    {
        return result;
    }

    public void setResult (String result)
    {
        this.result = result;
    }

    public Data[] getData ()
    {
        return data;
    }

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

    public Edit getEdit ()
    {
        return edit;
    }

    public void setEdit (Edit edit)
    {
        this.edit = edit;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [responseMessage = "+responseMessage+", result = "+result+", data = "+data+", edit = "+edit+"]";
    }
}

希望这会有所帮助。

答案 1 :(得分:1)

以后,您可以使用this链接为JSON创建POJO类。但是,您的JSON也是无效的,它在您的编辑对象中包含一个额外的

"edit":{
    "id":"156",
    "user_id":"5466",<--This comma

}

答案 2 :(得分:-1)

通过这种方式制作pojo类。

public class DataItem{

@SerializedName("dataid")
private String dataid;

@SerializedName("dataname")
private String dataname;

public void setDataid(String dataid){
    this.dataid = dataid;
}

public String getDataid(){
    return dataid;
}

public void setDataname(String dataname){
    this.dataname = dataname;
}

public String getDataname(){
    return dataname;
}

@Override
public String toString(){
    return 
        "DataItem{" + 
        "dataid = '" + dataid + '\'' + 
        ",dataname = '" + dataname + '\'' + 
        "}";
    }
}


public class Edit{

@SerializedName("user_id")
private String userId;

@SerializedName("id")
private String id;

public void setUserId(String userId){
    this.userId = userId;
}

public String getUserId(){
    return userId;
}

public void setId(String id){
    this.id = id;
}

public String getId(){
    return id;
}

@Override
public String toString(){
    return 
        "Edit{" + 
        "user_id = '" + userId + '\'' + 
        ",id = '" + id + '\'' + 
        "}";
    }
 }


public class Response{

@SerializedName("result")
private String result;

@SerializedName("data")
private List<DataItem> data;

@SerializedName("edit")
private Edit edit;

@SerializedName("responseMessage")
private String responseMessage;

public void setResult(String result){
    this.result = result;
}

public String getResult(){
    return result;
}

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

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

public void setEdit(Edit edit){
    this.edit = edit;
}

public Edit getEdit(){
    return edit;
}

public void setResponseMessage(String responseMessage){
    this.responseMessage = responseMessage;
}

public String getResponseMessage(){
    return responseMessage;
}

@Override
public String toString(){
    return 
        "Response{" + 
        "result = '" + result + '\'' + 
        ",data = '" + data + '\'' + 
        ",edit = '" + edit + '\'' + 
        ",responseMessage = '" + responseMessage + '\'' + 
        "}";
    }
 }

使用 Robopojo 插件生成pojo类。