javajsonandroid-studioretrofit2

Loop through JSON array from API Retrofit


I'm having problem with parsing my JSON Array from an API call. The response from the API look like this

{
"SUCCESS": [
    {
        "user_id": "susan",
        "email": "susan@gmail.com"
    },
    {
        "user_id": "andy",
        "email": "andy@gmail.com"
    },
    {
        "user_id": "john",
        "email": "john@gmail.com"
    }
  ]
}

I am using Retrofit2 and a class to handle the response

class userAPI {

    private userSuccess SUCCESS;

    public userSuccess getSUCCESS() {
        return SUCCESS;
    }
}

class userSuccess {
    private String user_id;
    private String email;

    public String getUser_id() {
        return user_id;
    }

    public String getEmail() {
        return email;
    }
}

Any clue on how to loop through the array inside the SUCCESS JSON Object? So far what I did is parsing the JSON as a List

class userAPI {

    public List<userSuccess> SUCCESS;

    public class userSuccess {

        public String user_id;
        public String email_user;

    }
}

And looping it with the following code

String displayResponse = "";
userAPI resource = response.body();

List<userAPI.userSuccess> dataList = resource.SUCCESS;

for (userAPI.userSuccess data : dataList) {
    displayResponse += data.user_id + " " + data.email_user + "\n";
}

Log.d("response ", displayResponse);

And here is the result

susan susan@gmail.com
andy andy@gmail.com
john john@gmail.com

But I wanted to store it as an array so I can loop through it by searching the user_id value and list it to Expandable List View


Solution

  • If you don't mind me re-writing your beans completely, here is an alternative way to represent the JSON as objects:

    import java.util.List;
    
    public class UserResponse {
      private List<User> SUCCESS;
    
      public List<User> getSuccess() {
        return SUCCESS;
      }
    
      public void setSuccess(List<User> success) {
        this.SUCCESS = success;
      }
    
      public static class User {
        private String user_id;
        private String email;
    
        public String getUserId() {
            return user_id;
        }
    
        public void setUserId(String userId) {
            this.user_id = userId;
        }
    
        public String getEmail() {
            return email;
        }
    
        public void setEmail(String email) {
            this.email = email;
        }
      }
    }
    

    Here's some example code, using Gson to take a JSON string and turn it into these objects:

    import com.google.gson.Gson;
    
    public class Main {
      public static void main(String[] args) {
        String json = "{\"SUCCESS\": [{\"user_id\": \"susan\",\"email\": \"susan@gmail.com\"},{\"user_id\": \"andy\",\"email\": \"andy@gmail.com\"},{\"user_id\": \"john\",\"email\": \"john@gmail.com\"}]}";
    
        Gson gson = new Gson();
        UserResponse userResponse = gson.fromJson(json, UserResponse.class);
    
        List<UserResponse.User> users = userResponse.getSuccess();
        for (UserResponse.User user : users) {
            String userId = user.getUserId();
            String email = user.getEmail();
            System.out.println("User ID: " + userId + ", Email: " + email);
        }
      }
    }