2

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!!

9
  • In your response children has only one item means children size is 1, thats why it is iterating only onse.. Commented May 20, 2016 at 10:07
  • I have made a separate object: JSONArray childrenProducts = jsono.getJSONArray("products"); this is the second array it is odd that this code is nt working.. Commented May 20, 2016 at 10:08
  • What value you are getting for childrenProducts.length()?? Commented May 20, 2016 at 10:10
  • 4 is what i am getting when i print childrenProducts.length() Commented May 20, 2016 at 10:15
  • 1
    According to your response length should be 3.. Are you getting data in jsonData? Commented May 20, 2016 at 10:19

3 Answers 3

1

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!

Sign up to request clarification or add additional context in comments.

2 Comments

If it first prints the data atleast then i can proceed with what you said but it isnt printing the data at all..
You've the json string right? You can go with this procedure anyway. There might be a problem with your loop or parsing. Let GSON to do the parsing for you. You'll just get the values from mResponse class and then try printing them. Its easy to use and I almost wrote the full code for you.
1

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") + "<----");
    }

3 Comments

it wasn't working even when vendor had a value but i think it was maybe because of some exception
exactly.. This will work if you don't put two try.. catch..!!
I have edited to be correct..!! This was the actual problem.. please accept it if it explains the same issue..!!
0

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

Comments

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.