-1

I am trying to parse the json object to get the dynamic keys and values and set them to RecyclerView in my application. And also I want to get the key or value when user clicks the recyclerview child.

{
"status": 200,
"message_type": "info",
    "car_types": {
        "sports_car": "BMW", 
        "sports_car": "lamborghini",
        "classic_car": "benz",
        "luxary_car": "bently cooper",
        "suv": "Range Rover",
        "Other": "Other"
    }
}
D.M
  • 510
  • 6
  • 14
Krishna Ch
  • 1,471
  • 2
  • 11
  • 14

1 Answers1

0

Try this following,To get key and value from the JSONObject.

   try {

                JSONObject jsonObject = new JSONObject("your result");

                JSONObject jsonObject1_message = jsonObject.getJSONObject("message_type");

                Map<String,String> map = new HashMap<String,String>();
                Iterator iter = jsonObject1_message.keys();

                while(iter.hasNext()){
                    String key = (String)iter.next();
                    String value = jsonObject1_message.getString(key);
                    map.put(key,value);

                    Log.d("keywithvalues","print    "+key+"   "+value);

                }



            }catch (Exception e)
            {
                        Log.d("error","**   "+e.toString());
            }

I hope this may help you.

Gowthaman M
  • 8,057
  • 8
  • 35
  • 54