How do I get specific values ​​from the JSON answer using the Android code?

advertisements

I need to get Particular Object values ( A, B, C, D) and related key values (@"name" ). After getting (A, B, C, D ) object values I need to list out into list view Android. Here below I have posted my sample code and response. Please help me.

@Override
        protected Void doInBackground(Void... arg0) {
            // Creating service handler class instance
            ServiceHandler sh = new ServiceHandler();

            // Making a request to url and getting response
            String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
            Log.d("Response: ", "> " + jsonStr);

            if (jsonStr != null) {
                try {
                    JSONObject jsonObj = new JSONObject(jsonStr);

                    contacts = jsonObj.getJSONArray("response");
                    Log.d("Response: ", "> " + contacts);

                    // looping through All Contacts
                    for (int i = 0; i < contacts.length(); i++) {
                        JSONObject c = contacts.getJSONObject(i);

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } else {
                Log.e("ServiceHandler", "Couldn't get any data from the url");
            }

            return null;
        }

My JSON Response :

{"response" : [  {

                "A"   :  [   {
                         "name" : "tango"
                           },
                       {
                        "name" : "ping"
                       }
                     ],

                "B"  :  [   {
                         "name" : "tango"
                           },
                       {
                        "name" : "ping"
                       }
                    ]
             } ]}


Use JSONObject keys() to get the key and then iterate each key to get to the dynamic value.

You can get dynamic keys like this

JSONObject responseDataObj = new JSONObject(responseData);
JSONArray responseArray = responseDataObj.getJSONArray("response");
for (int i = 0; i < responseArray.length(); i++) {
    nodes = new ArrayList<ArrayList<String>>();//nodes ArrayList<ArrayList<String>> declared globally
    nodeSize = new ArrayList<Integer>();//nodeSize ArrayList<Integer> declared globally
    JSONObject obj = responseArray.getJSONObject(i);
    Iterator keys = obj.keys();
    while(keys.hasNext()) {
       // loop to get the dynamic key
       String currentDynamicKey = (String)keys.next();
       //store key in an arraylist which is A,B,...
       // get the value of the dynamic key
       JSONArray currentDynamicValue = obj.getJSONArray(currentDynamicKey);
       int jsonrraySize = currentDynamicValue.length();
       int sizeInArrayList = jsonrraySize + 1;
       nodeSize.add(sizeInArrayList);
       if(jsonrraySize > 0) {
           for (int ii = 0; ii < jsonrraySize; ii++) {
                nameList = new ArrayList<String>();//nameList ArrayList<String> declared globally
               if(ii == 0) {
                JSONObject nameObj = currentDynamicValue.getJSONObject(ii);
                String name = nameObj.getString("name");
                System.out.print("Name = " + name);
                //store name in an arraylist
                nameList.add(name);
              }
           }
       }
     nodes.add(nameList);
    }
}