使用翻新的具有多个数组的对象

时间:2018-10-07 16:05:06

标签: android

我正在使用android和一个节点API,JSON对象具有多个数组,如下所示。这是我需要在Android中使用翻新处理的以下响应。

    {
    "NewUser": [
        {
            "UserID": 001,
            "Name": "Linda",
            "Surname": "Curry"

        }
    ],
    "ExistingUser": [
        {
             "UserID": 002,
            "Name": "Jim",
            "Surname": "Noah"
        }
    ],
    "ArchivedUser": [
        {
            "UserID": 003,
            "Name": "Nina",
            "Surname": "Ivanka"
        }
    ]
}
}

我不确定如何设置我的POJO类以及如何访问单个列表。 在此先感谢您,为此努力了几天。

1 个答案:

答案 0 :(得分:1)

这将是您需要使用的主要POJO类。子类在下面发布。如您所见,您可以在代码中相应地访问各个列表。例如,您想要NewUser列表,则可以在下面的列表中访问它。

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

public class Example {

@SerializedName("NewUser")
@Expose
private List<NewUser> newUser = null;
@SerializedName("ExistingUser")
@Expose
private List<ExistingUser> existingUser = null;
@SerializedName("ArchivedUser")
@Expose
private List<ArchivedUser> archivedUser = null;

public List<NewUser> getNewUser() {
return newUser;
}

public void setNewUser(List<NewUser> newUser) {
this.newUser = newUser;
}

public List<ExistingUser> getExistingUser() {
return existingUser;
}

public void setExistingUser(List<ExistingUser> existingUser) {
this.existingUser = existingUser;
}

public List<ArchivedUser> getArchivedUser() {
return archivedUser;
}

public void setArchivedUser(List<ArchivedUser> archivedUser) {
this.archivedUser = archivedUser;
}

}

这是ArchivedUser类:

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class ArchivedUser {

@SerializedName("UserID")
@Expose
private Integer userID;
@SerializedName("Name")
@Expose
private String name;
@SerializedName("Surname")
@Expose
private String surname;

public Integer getUserID() {
return userID;
}

public void setUserID(Integer userID) {
this.userID = userID;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getSurname() {
return surname;
}

public void setSurname(String surname) {
this.surname = surname;
}

}

这是ExistingUser类:

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class ExistingUser {

@SerializedName("UserID")
@Expose
private Integer userID;
@SerializedName("Name")
@Expose
private String name;
@SerializedName("Surname")
@Expose
private String surname;

public Integer getUserID() {
return userID;
}

public void setUserID(Integer userID) {
this.userID = userID;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getSurname() {
return surname;
}

public void setSurname(String surname) {
this.surname = surname;
}

}

这是NewUser类:

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class NewUser {

@SerializedName("UserID")
@Expose
private Integer userID;
@SerializedName("Name")
@Expose
private String name;
@SerializedName("Surname")
@Expose
private String surname;

public Integer getUserID() {
return userID;
}

public void setUserID(Integer userID) {
this.userID = userID;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getSurname() {
return surname;
}

public void setSurname(String surname) {
this.surname = surname;
}

}