简体   繁体   中英

Iterating through JSonArray response from url using android volley

This is my JSonArray

{  
   "vendor":[  
      {  
         "vendor_name":"Tapan Moharana",
         "vendor_description":"",
         "vendor_slug":"tapan",
         "vendor_logo":null,
         "contact_number":null
      }
   ],
   "products":[  
      {  
         "name":"Massage",
         "price":"5000.0000",
         "image":"http:\/\/carrottech.com\/lcart\/media\/catalog\/product\/cache\/1\/image\/150x\/9df78eab33525d08d6e5fb8d27136e95\/2\/9\/29660571-beauty-spa-woman-portrait-beautiful-girl-touching-her-face.jpg"
      },
      {  
         "name":"Chicken Chilly",
         "price":"234.0000",
         "image":"http:\/\/carrottech.com\/lcart\/media\/catalog\/product\/cache\/1\/image\/150x\/9df78eab33525d08d6e5fb8d27136e95\/c\/h\/cheicken.jpg"
      },
      {  
         "name":"Chicken Biryani",
         "price":"500.0000",
         "image":"http:\/\/carrottech.com\/lcart\/media\/catalog\/product\/cache\/1\/image\/150x\/9df78eab33525d08d6e5fb8d27136e95\/placeholder\/default\/image_1.jpg"
      }
   ]
}

and this is my java code:

    JSONObject jsono = new JSONObject(response);
    JSONArray children = jsono.getJSONArray("vendor");
    JSONArray childrenProducts = jsono.getJSONArray("products");
    for (int i = 0; i <children.length(); i++) {
        JSONObject jsonData = children.getJSONObject(i);
        System.out.print(jsonData.getString("vendor_name") + "<----");
        //  String vendorThumbNailURL=jsonData.getString("")
        //jvendorImageURL.setImageUrl(local, mImageLoader);
        vendorLogo=vendorLogo+jsonData.getString("vendor_logo").trim();
        jvendorImageURL.setImageUrl(vendorLogo, mImageLoader);
        jvendorName.setText(jsonData.getString("vendor_name"));
        jvendorAbout.setText(jsonData.getString("vendor_description"));
        jvendorContact.setText(jsonData.getString("contact_number"));
        System.out.print(jsonData.getString("products") + "<----");
    }
    for(int i=0;i<childrenProducts.length();i++){
        JSONObject jsonData = childrenProducts.getJSONObject(i);
        System.out.println("inside products");
        System.out.print(jsonData.getString("name") + "<----dd");
    }

The first for loop is working fine but the second forloop is not.. i dont get anything if i try to execute those print statements inside the second for loop.. please help me!!

Why don't you use Gson to get parse the JSON string simply?

You need to declares classes first to match the JSON response like this.

public class Vendor {

    private String vendor_name;
    private String vendor_description;
    private String vendor_slug;
    private String vendor_logo;
    private String contact_number;

    public Vendor() {
    }

    public String getVendor_name() {
        return vendor_name;
    }

    public String getVendor_description() {
        return vendor_description;
    }

    public String getVendor_slug() {
        return vendor_slug;
    }

    public String getVendor_logo() {
        return vendor_logo;
    }

    public String getContact_number() {
        return contact_number;
    }
}

...

public class Product {

    private String name;
    private String price;
    private String image;

    public Product() {
    }

    public String getName() {
        return name;
    }

    public String getPrice() {
        return price;
    }

    public String getImage() {
        return image;
    }
}

Now declare the Response class like this

public class Response {

    private List<Vendor> vendor;
    private List<Product> products;

    public Response() {
    }

    public List<Vendor> getVendor() {
        return vendor;
    }

    public List<Product> getProducts() {
        return products;
    }
}

Now, once you've the JSON string its easy to bounce the data using GSON into the Response class like this.

Gson gson = new Gson();
Response mResponse = gson.fromJson(jsonString, Response.class);

Simple!

It is not going in your second for loop because there is SomeException in your first for loop.

Your execution will be thrown out to any catch() clause, and the further execution will not be done including your second for loop.

Just try putting that up like this:

    JSONObject jsono = new JSONObject(response);
    JSONArray children = jsono.getJSONArray("vendor");
    JSONArray childrenProducts = jsono.getJSONArray("products");

    //this will be executed now..!!

    for(int i=0;i<childrenProducts.length();i++){
        JSONObject jsonData = childrenProducts.getJSONObject(i);
        System.out.println("inside products");
        System.out.print(jsonData.getString("name") + "<----dd");
    }

    for (int i = 0; i <children.length(); i++) {
        JSONObject jsonData = children.getJSONObject(i);
        System.out.print(jsonData.getString("vendor_name") + "<----");
        //  String vendorThumbNailURL=jsonData.getString("")
        //jvendorImageURL.setImageUrl(local, mImageLoader);
        vendorLogo=vendorLogo+jsonData.getString("vendor_logo").trim();
        jvendorImageURL.setImageUrl(vendorLogo, mImageLoader);
        jvendorName.setText(jsonData.getString("vendor_name"));
        jvendorAbout.setText(jsonData.getString("vendor_description"));
        jvendorContact.setText(jsonData.getString("contact_number"));
        System.out.print(jsonData.getString("products") + "<----");
    }

This is how i solved it:

   try {
                    JSONObject jsono = new JSONObject(response);
                    JSONArray children = jsono.getJSONArray("vendor");
                    for (int i = 0; i <children.length(); i++) {
                        JSONObject jsonData = children.getJSONObject(i);
                        System.out.print(jsonData.getString("vendor_name") + "<----");
                      //  String vendorThumbNailURL=jsonData.getString("")
                        //jvendorImageURL.setImageUrl(local, mImageLoader);
                        vendorLogo=vendorLogo+jsonData.getString("vendor_logo").trim();
                        jvendorImageURL.setImageUrl(vendorLogo, mImageLoader);
                        jvendorName.setText(jsonData.getString("vendor_name"));
                        jvendorAbout.setText(jsonData.getString("vendor_description"));
                        jvendorContact.setText(jsonData.getString("contact_number"));
                        System.out.print(jsonData.getString("products") + "<----");
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }

                try {
                    JSONObject jsono = new JSONObject(response);
                    JSONArray childrenProducts = jsono.getJSONArray("products");
                    System.out.println(childrenProducts.length()+"LENGTH");
                    for(int i=0; i<childrenProducts.length(); i++){
                        JSONObject jsonData1 = childrenProducts.getJSONObject(i);
                        System.out.println(childrenProducts.length() + "LENGTH");
                        System.out.print(jsonData1.getString("name") + "<----dd");
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

just had to take two separate try blocks... can someone please tell why was it not working in one try block? the above code works

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