2

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"
                       }
                    ]
             } ]}
6
  • your response is not a valid json Commented Nov 10, 2014 at 5:56
  • Hello @KetanAhir Could you please check now my response... Commented Nov 10, 2014 at 6:15
  • what you want to display in listview Commented Nov 10, 2014 at 6:25
  • I want to get (A, B , C, D) objects from reponse and get first index Name value then list out A and Name, B and Name,... Commented Nov 10, 2014 at 6:29
  • @Mano there is no key for JSONObject inside response JSONArray is this ok ? Commented Nov 10, 2014 at 6:53

3 Answers 3

4

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);
    }
}
Sign up to request clarification or add additional context in comments.

10 Comments

Wow Great.. This is I am expected absolute Answer.... Thank you so much @Kaushik....I need to list out A B C vlaues into the list view...
store that in last for loop
@kaushik...I need another one clarification. Here the code I can get A, B , C values with related name values. But there multiple name values available within A and B. This time I need to get A & B of first index name value only. Please clarify this doubt....
@Mano : u need only 1st name from both A and B
check the edit though I used ArrayList<ArrayList<String>> it is better if u use a ArrayList of POJO class
|
0

You can use following method to parse your response and handle output as per requirement.

private void parseJson(String res) {
    try {
        JSONObject mainObject = new JSONObject(res);
        JSONArray response = mainObject.getJSONArray("response");
        for (int i = 0; i < response.length(); i++) {
            JSONObject obj = response.getJSONObject(i);

            JSONArray A = obj.getJSONArray("A");

            for (int j = 0; j < A.length(); j++) {
                JSONObject objA = A.getJSONObject(j);
                String name = objA.getString("name");
                // use or store name here
            }

            JSONArray B = obj.getJSONArray("B");

            for (int k = 0; k < B.length(); k++) {
                JSONObject objB = B.getJSONObject(k);
                String name = objB.getString("name");
                // use or store name here
            }
        }
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

3 Comments

Thanks for your Quick replay @KetanAhir. BTW the problem is we dont know That object is A, B,C,... There some chances for changing other values.... I cant specify the object name A , B, C, ...Particularly...
here A,B,C are array..so it must be known.
No. I just posted sample response. I cant specify particularly A,B,C... Because I have received different type of names...
0

This may work for you :

        String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
        Log.d("Response: ", "> " + jsonStr);
            if (jsonStr != null) {
                try {
                    JSONObject jsonObj = new JSONObject(jsonStr);
                    JSONArray contacts;
                    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);
                        JSONArray jsonArrayA=new JSONArray();
                        jsonArrayA=c.getJSONArray("A");
                        for(int j=0;j<jsonArrayA.length();j++){
                            String name=jsonArrayA.getJSONObject(j).getString("name");
                            Log.e("Name","name of jsonarray A "+i+" "+name);
                        }

                        JSONArray jsonArrayB=c.getJSONArray("B");
                        for(int k=0;k<jsonArrayB.length();k++){
                            String name=jsonArrayB.getJSONObject(k).getString("name");
                            Log.e("Name","name of jsonarray B "+i+" "+name);
                        }

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

2 Comments

Thank you so much for your quick replay. There actually The A B C names I cant put directly into the code because I have received different type of names from reponse.
Put that name in A B C place, and it will do

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.