-1

I have the json array :

{"objets":{"1":"Question","2":"Response"},"success":true}

I want to test if the text selected on spinner exist in the array it will return the number (1 or 2).

Here is what I've done until now :

String responseContent = response.asString();
                                    Log.d("OBJECTS", responseContent);
                                    try {
                                        JSONObject jobj = new JSONObject(responseContent);
                                        JSONObject users = jobj.getJSONObject("objetcs");
                                        Log.e("hello", String.valueOf(users));

                                        if(users.toString().contains(spinner_objet.getSelectedItem().toString().trim())){
                                            Log.e("hello", "it exist");
                                            String user=users.getString("id");
                                            Log.e("hello1", String.valueOf(user));
                                        }


                                    } catch (JSONException e1) {
                                        e1.printStackTrace();
                                    }

But I'm not getting any thing, can any one help me with this ?

1 Answers1

0

You can parse your Json into a hashMap like so :

JSONObject object = new JSONObject();
Map<String,Integer> map = new HashMap<>();
while (object.keys().hasNext() ){
    Integer key = (Integer) object.keys().next();
    map.put((String) object.get(String.valueOf(key)), key);
}

You can then use the values of the map to put in the spinner.

map.values();

Whenever the user selects from the spinner, you can get the id from the map.

Gautam
  • 1,862
  • 9
  • 16