0

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.

5
  • 2
    try using jsonschema2pojo.org Commented Oct 7, 2018 at 16:14
  • was it solved? @StarDust Commented Oct 7, 2018 at 16:42
  • @HaiderAli it gave me 3 separate classes using Hash Maps so now how will my interface include all three classes? Commented Oct 7, 2018 at 17:09
  • @PrashanthVerma , not quite. I know how to do if the object is one array. But this seems to change everything. All i need is the structure than i can take it from there Commented Oct 7, 2018 at 17:17
  • Check out the answer and let me know if you have a problem Commented Oct 7, 2018 at 18:44

1 Answer 1

1

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

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.