Search code examples
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": "[email protected]"
    },
    {
        "user_id": "andy",
        "email": "[email protected]"
    },
    {
        "user_id": "john",
        "email": "[email protected]"
    }
  ]
}

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 [email protected]
andy [email protected]
john [email protected]

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\": \"[email protected]\"},{\"user_id\": \"andy\",\"email\": \"[email protected]\"},{\"user_id\": \"john\",\"email\": \"[email protected]\"}]}";
    
        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);
        }
      }
    }