0

I have successfully Integrated Volley library into my android studio project and got below responses. Now I need to get particular values from below response. I need to GET (A, B, C, D, E,etc,.. )values and related boys and girls values also. Once I got it I need to list out on list view like below model.

I am expecting :

-----------------------------------------
   Title        Boys            Girls
-----------------------------------------
     A          1.5                3.5
-----------------------------------------
     B          1.5                3.5
-----------------------------------------

Below My Code :

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //Displays Home Screen
        setContentView(R.layout.activity_list);

        //displaying TextView
        txtDisplay = (TextView) findViewById(R.id.txtDisplay);

        RequestQueue queue = Volley.newRequestQueue(this);
        String url = "MYURL";

        JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {

            @Override
            public void onResponse(JSONObject response) {
                // TODO Auto-generated method stub
                txtDisplay.setText("Response => "+response.toString());
                System.out.println("Response => "+response.toString());
                findViewById(R.id.progressBar1).setVisibility(View.GONE);
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                // TODO Auto-generated method stub

            }
        });

        queue.add(jsObjRequest);
    }

Below My Response :

{ 
   "response":{ 
      "A":{ 
         "boys":1.5,
         "girls":"3.5",
      },
      "B":{ 
         "boys":1.5,
         "girls":"3.5",
      },
      "E":{ 
         "boys":1.5,
         "girls":"3.5",
      }
  },
   "schools":{ 
   },
   "distances":"172 KM"
}
SteaveJobs
  • 33
  • 2
  • 8
  • Welcome. Could you describe in your post what is not working? – wwkudu Oct 27 '15 at 07:18
  • Hi. Everything working fine. I need to get particular values from above response and list it out on list view. – SteaveJobs Oct 27 '15 at 07:19
  • Ok. What have you tried so far? Where are you stuck? – wwkudu Oct 27 '15 at 07:20
  • Getting particular values. Thats the plm. Give some code for how to get particular JSONObject from JSON reponse. – SteaveJobs Oct 27 '15 at 07:22
  • SO works best when you have a particular place where you are stuck, you show what you have tried so far. Can I suggest you try a web search first. A quick search returned things like [this example](http://www.javacodegeeks.com/2013/10/android-json-tutorial-create-and-parse-json-data.html), [this](http://stackoverflow.com/questions/5566669/how-to-parse-a-json-object-in-android), [this](http://stackoverflow.com/questions/7634518/getting-jsonobject-from-jsonarray), ... Do they help at all? – wwkudu Oct 27 '15 at 07:26

3 Answers3

4

use following code to get values:

JsonObject json = response.get("response");
String a =json.get("A").toString();

similarly find other values;

eg;

when you get

JsonObject json = response.get("response");

then the json will be :

{ 
      "A":{ 
         "boys":1.5,
         "girls":"3.5",
      },
      "B":{ 
         "boys":1.5,
         "girls":"3.5",
      },
      "E":{ 
         "boys":1.5,
         "girls":"3.5",
      }
  }

and if you want to get A / B/ C from this json use

jsonA = json.get("A");


          { 
             "boys":1.5,
             "girls":"3.5",
          }

if you want to retrive boys from A then

String boys = jsonA.get("boys").toString();

similarly

json.get("B");

and

json.get("C");

Gaurav
  • 3,615
  • 2
  • 27
  • 50
0

In order to get the string from the json object you can use the following

  JSONObject jObj=new JSONObject(response.toString());

for boys of type a

 String a = jObj.getJSONObject("response").getJSONObject("A").getString("boys").toString();

for girls of type a

 String b = jObj.getJSONObject("response").getJSONObject("A").getString("girls").toString();

run the follwoing lines in a loop and store them according to your needs

Cloy
  • 2,141
  • 23
  • 32
  • I cant mention "A" and "B" and "C" because may be It will go upto Z. without mentioning object values, how to get all values and listing out! – SteaveJobs Oct 27 '15 at 10:23
0

I got it myself, What I am expected.

JSONObject data = response.getJSONObject("response");
Iterator keys = data.keys();

while(keys.hasNext()) {
        // loop to get the dynamic key
        String currentDynamicKey = (String)keys.next();

        // get the value of the dynamic key
        JSONObject currentDynamicValue = data.getJSONObject(currentDynamicKey);

        // do something here with the value...
    }
SteaveJobs
  • 33
  • 2
  • 8