简体   繁体   中英

How to get Json Array in Json Object using volley in android and set this json data in listview code and Xml Avilavble

I am using volley for JSON parsing. I want to get some data using GET from server side, that I tried to send. Can any one tell me how? Following is my codes. I also tried HashMap and JSONobject but getting this error.

of type org.json.JSONObject cannot be converted to JSONArray

and more, please tell me why.

JSON

{
 "Baby Care": [{
  "id": "35",
  "sub_category": "Baby Food"
 }, {
  "id": "36",
  "sub_category": "Diapers & Wipes"
 }, {
  "id": "38",
  "sub_category": "Baby Accessories"
 }],
 "Personal Care": [{
  "id": "27",
  "sub_category": "Skin Care"
 }, {
  "id": "28",
  "sub_category": "Hair Care"
 }, {
  "id": "29",
  "sub_category": "Oral Care"
 }, {
  "id": "30",
  "sub_category": "Bath, Face & Hand Wash"
 }],
 "Grocery": [{
  "id": "51",
  "sub_category": "Cheese"
 }, {
  "id": "52",
  "sub_category": "Cakes & Toast"
 }, {
  "id": "63",
  "sub_category": "Beverages"
 }, {
  "id": "69",
  "sub_category": "Dal & Pulses"
 }, {
  "id": "70",
  "sub_category": "Rice"
 }],
 "Household": [{
  "id": "59",
  "sub_category": "Kitchen Appliances"
 }, {
  "id": "60",
  "sub_category": "Vacuum and Floor care"
 }],
 "Women": [{
  "id": "64",
  "sub_category": "Jewellery"
 }, {
  "id": "65",
  "sub_category": "Make-up"
 }, {
  "id": "66",
  "sub_category": "Perfume"
 }],
 "Men": [{
  "id": "67",
  "sub_category": "Perfume"
 }, {
  "id": "68",
  "sub_category": "Accessories"
 }]
}

How can I get this JSON format and set into ListView using Volley I tried the code below but I didn't get any objects:

JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(productUrl, new Response.Listener < JSONArray > () {
 @Override
 public void onResponse(JSONArray response) {
  try {
   for (int i = 0; i < response.length(); i++) {

    JSONArray ja = response.getJSONArray(i);
    for (int j = 0; j < ja.length(); j++) {
     JSONObject jb = ja.getJSONObject(j);
     SubProduct movie = new SubProduct();
     movie.setId(jb.getInt("id"));
     movie.setname(jb.getString("sub_category"));

     movieList.add(movie);
    }
   }
   adapter.notifyDataSetChanged();
   progressDialog.dismiss();

  } catch (JSONException e) {
   e.printStackTrace();
   progressDialog.dismiss();
  }
 }
}, new Response.ErrorListener() {
 @Override
 public void onErrorResponse(VolleyError error) {
  Log.e("Volley", error.toString());
  progressDialog.dismiss();
 }
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(jsonArrayRequest);

use :

JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.GET,
                productUrl, null,
                new Response.Listener<JSONObject>() {

instead of :

   JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(productUrl, new Response.Listener<JSONArray>(){

use below code for getting dynamic keys for array:

Iterator<?> keys = response.keys();
while( keys.hasNext() ) {
    String key = (String)keys.next();
    if ( response.get(key) instanceof JSONObject ) {
        System.out.println(key); // do whatever you want with it
    }
}

and you can get objects by using following code:

JSONArray arr = response.getJSONArray(key);
    JSONObject element;
    for(int i = 0; i < arr.length(); i++){
        element = arr.getJSONObject(i);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM