简体   繁体   中英

Object with multiple arrays using Retrofit

I'm working with android and a node API, the JSON object has multiple arrays as follows. This is the following response I need to handle in android using retrofit.

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

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

I'm not sure how to set up my POJO class and how to access the individual lists. Thanks in advance, been struggling with this for a couple of days now.

This would be your main POJO class that you need to use. The subclasses are posted below. As you can see, you can access individual lists accordingly in code. For example you want NewUser list then you can access it in List given below.

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

}

This is the ArchivedUser Class :

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

}

This is the ExistingUser class:

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

}

This is the NewUser class:

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

}

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