简体   繁体   English

使用Android Volley从URL遍历JSonArray响应

[英]Iterating through JSonArray response from url using android volley

This is my JSonArray 这是我的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: 这是我的Java代码:

    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!! 第一个for循环工作正常,但第二个forloop则工作不正常。如果我尝试在第二个for循环内执行这些打印语句,我什么也没得到。

Why don't you use Gson to get parse the JSON string simply? 为什么不使用Gson来简单地解析JSON字符串?

You need to declares classes first to match the JSON response like this. 您需要先声明类,以匹配JSON响应,如下所示。

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 现在像这样声明Response

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. 现在,一旦有了JSON字符串,就可以像这样使用GSON轻松将数据反弹到Response类中。

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. 它不在您的第二个for循环中进行,因为您的第一个for循环中存在SomeException

Your execution will be thrown out to any catch() clause, and the further execution will not be done including your second for loop. 您的执行将被抛出到任何catch()子句中,并且进一步的执行将不会完成,包括您的第二个for循环。

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 上面的代码有效

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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