0

hi everyone i really need you help for this one i'm getting this json response using get request with retrofit :

 [{
"id": 2,
"name": "admin",
"password_last_update": null,
"phone": "2516512",
"phone2": null,
"mobile": null,
"realname": "zizo",
"firstname": null,
"locations_id": 0,
"language": null,
"use_mode": 0,
"list_limit": 20,
"is_active": 1,
"comment": null,
"auths_id": 0,
"authtype": 1,
"last_login": "2021-06-14 17:36:58",
"date_mod": "2021-06-14 16:29:50",
"date_sync": null,
"is_deleted": 0,
"profiles_id": 0,
"entities_id": 0,
"usertitles_id": 0,
"usercategories_id": 0,
"date_format": null,
"number_format": null,
"names_format": null,
"csv_delimiter": null,
"is_ids_visible": null,
"use_flat_dropdowntree": null,
"show_jobs_at_login": null,
"priority_1": null,
"priority_2": null,
"priority_3": null,
"priority_4": null,
"priority_5": null,
"priority_6": null,
"followup_private": null,
"task_private": null,
"default_requesttypes_id": null,
"password_forget_token": null,
"password_forget_token_date": null,
"user_dn": null,
"registration_number": null,
"show_count_on_tabs": null,
"refresh_views": null,
"set_default_tech": null,
"personal_token_date": null,
"api_token_date": null,
"cookie_token_date": "2021-06-14 16:29:50",
"display_count_on_home": null,
"notification_to_myself": null,
"duedateok_color": null,
"duedatewarning_color": null,
"duedatecritical_color": null,
"duedatewarning_less": null,
"duedatecritical_less": null,
"duedatewarning_unit": null,
"duedatecritical_unit": null,
"display_options": null,
"is_deleted_ldap": 0,
"pdffont": null,
"picture": null,
"begin_date": null,
"end_date": null,
"keep_devices_when_purging_item": null,
"privatebookmarkorder": null,
"backcreated": null,
"task_state": null,
"layout": null,
"palette": null,
"set_default_requester": null,
"lock_autolock_mode": null,
"lock_directunlock_notification": null,
"date_creation": null,
"highcontrast_css": null,
"plannings": null,
"sync_field": null,
"groups_id": 0,
"users_id_supervisor": 0,
"timezone": null,
"default_dashboard_central": null,
"default_dashboard_assets": null,
"default_dashboard_helpdesk": null,
"default_dashboard_mini_ticket": null,
"links": [
  {
    "rel": "Entity",
    "href": "https://aziz.with7.glpi-network.cloud/apirest.php/Entity/0"
  },
  {
    "rel": "Document_Item",
    "href": "https://aziz.with7.glpi-network.cloud/apirest.php/User/2/Document_Item/"
  }
  ]
  },
   {
   "id": 6,
   "name": "aziz",
"password_last_update": "2021-05-31 12:01:43",
"phone": "",
"phone2": "",
"mobile": "",
"realname": "aziz",
"firstname": "",
"locations_id": 0,
"language": null,
"use_mode": 0,
"list_limit": null,
"is_active": 1,
"comment": "",
"auths_id": 0,
"authtype": 1,
"last_login": "2021-06-14 16:48:52",
"date_mod": "2021-06-14 16:48:52",
"date_sync": null,
"is_deleted": 0,
"profiles_id": 0,
"entities_id": 0,
"usertitles_id": 0,
"usercategories_id": 0,
"date_format": null,
"number_format": null,
"names_format": null,
"csv_delimiter": null,
"is_ids_visible": null,
"use_flat_dropdowntree": null,
"show_jobs_at_login": null,
"priority_1": null,
"priority_2": null,
"priority_3": null,
"priority_4": null,
"priority_5": null,
"priority_6": null,
"followup_private": null,
"task_private": null,
"default_requesttypes_id": null,
"password_forget_token": null,
"password_forget_token_date": null,
"user_dn": null,
"registration_number": "",
"show_count_on_tabs": null,
"refresh_views": null,
"set_default_tech": null,
"personal_token_date": null,
"api_token_date": null,
"cookie_token_date": "2021-06-14 16:48:52",
"display_count_on_home": null,
"notification_to_myself": null,
"duedateok_color": null,
"duedatewarning_color": null,
"duedatecritical_color": null,
"duedatewarning_less": null,
"duedatecritical_less": null,
"duedatewarning_unit": null,
"duedatecritical_unit": null,
"display_options": null,
"is_deleted_ldap": 0,
"pdffont": null,
"picture": "f7/6_60c3e5d2a69f7.jpg",
"begin_date": null,
"end_date": null,
"keep_devices_when_purging_item": null,
"privatebookmarkorder": null,
"backcreated": null,
"task_state": null,
"layout": null,
"palette": null,
"set_default_requester": null,
"lock_autolock_mode": null,
"lock_directunlock_notification": null,
"date_creation": "2021-05-31 12:01:43",
"highcontrast_css": null,
"plannings": null,
"sync_field": null,
"groups_id": 0,
"users_id_supervisor": 0,
"timezone": null,
"default_dashboard_central": null,
"default_dashboard_assets": null,
"default_dashboard_helpdesk": null,
"default_dashboard_mini_ticket": null,
"links": [
  {
    "rel": "Entity",
    "href": "https://aziz.with7.glpi-network.cloud/apirest.php/Entity/0"
  },
  {
    "rel": "Document_Item",
    "href": "https://aziz.with7.glpi-network.cloud/apirest.php/User/6/Document_Item/"
  }
]
},
{
"id": 7,
"name": "kaso",
"password_last_update": "2021-06-08 00:12:10",
"phone": "",
"phone2": "",
"mobile": "",
"realname": "ben Haj Laroussi",
"firstname": "Mohamed Aziz",
"locations_id": 0,
"language": null,
"use_mode": 0,
"list_limit": null,
"is_active": 1,
"comment": "",
"auths_id": 0,
"authtype": 1,
"last_login": "2021-06-13 17:18:45",
"date_mod": "2021-06-12 01:32:23",
"date_sync": null,
"is_deleted": 0,
"profiles_id": 0,
"entities_id": 0,
"usertitles_id": 0,
"usercategories_id": 0,
"date_format": null,
"number_format": null,
"names_format": null,
"csv_delimiter": null,
"is_ids_visible": null,
"use_flat_dropdowntree": null,
"show_jobs_at_login": null,
"priority_1": null,
"priority_2": null,
"priority_3": null,
"priority_4": null,
"priority_5": null,
"priority_6": null,
"followup_private": null,
"task_private": null,
"default_requesttypes_id": null,
"password_forget_token": null,
"password_forget_token_date": null,
"user_dn": null,
"registration_number": "",
"show_count_on_tabs": null,
"refresh_views": null,
"set_default_tech": null,
"personal_token_date": null,
"api_token_date": null,
"cookie_token_date": "2021-06-08 00:18:05",
"display_count_on_home": null,
"notification_to_myself": null,
"duedateok_color": null,
"duedatewarning_color": null,
"duedatecritical_color": null,
"duedatewarning_less": null,
"duedatecritical_less": null,
"duedatewarning_unit": null,
"duedatecritical_unit": null,
"display_options": null,
"is_deleted_ldap": 0,
"pdffont": null,
"picture": null,
"begin_date": null,
"end_date": null,
"keep_devices_when_purging_item": null,
"privatebookmarkorder": null,
"backcreated": null,
"task_state": null,
"layout": null,
"palette": null,
"set_default_requester": null,
"lock_autolock_mode": null,
"lock_directunlock_notification": null,
"date_creation": "2021-06-08 00:12:10",
"highcontrast_css": null,
"plannings": null,
"sync_field": null,
"groups_id": 0,
"users_id_supervisor": 0,
"timezone": null,
"default_dashboard_central": null,
"default_dashboard_assets": null,
"default_dashboard_helpdesk": null,
"default_dashboard_mini_ticket": null,
"links": [
  {
    "rel": "Entity",
    "href": "https://aziz.with7.glpi-network.cloud/apirest.php/Entity/0"
  },
  {
    "rel": "Document_Item",
    "href": "https://aziz.with7.glpi-network.cloud/apirest.php/User/7/Document_Item/"
  }
]
}
]

i'm using this code to get the data and to put it in a List :

    Retrofit retrofit= RetrofitInstance.getRetrofitInstance();
    final Api api= retrofit.create(Api.class);
    Call<List<User>> call = api.getUser(sestoken);
    call.enqueue(new Callback<List<User>>() {
        @Override
        public void onResponse(Call<List<User>> call, Response<List<User>> response) {

            if (response.code() != 200){
                Log.e("TAG", "onResponse: something is wrong"+response.code() );



            }
            List<User> users = response.body();



            for (User user : users){

                userList.add(user);
            }

            



           

        }

        @Override
        public void onFailure(Call<List<User>> call, Throwable t) {


        }
    });

and this is the user class package model;

public class User {

private String name;
private String id;


public String getName() { return name; }

public String getId() {
    return id;
}

}

i only need to put the id and name in a hashmap i really didn't know how to do it i could only put the data in a list . appreciate the help

1 Answer 1

1

Assuming that user id's are unique, you can create a simple HashMap with user id as the Key and user name as the Value.

Map<Integer, String> userMap = new HashMap<>();

for (User user : users) {
    if (userMap.get(user.getId()) == null)
        userMap.put(user.getId(), user.getName());
}

System.out.println("userMap : " + userMap);
Sign up to request clarification or add additional context in comments.

1 Comment

Could you please mark this answer as "Accepted" so that it can be of help to others.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.