繁体   English   中英

在json中使用附加的json字符串迭代json数组作为url使用volley的响应

[英]Iterating through json array with appended json string in android after json as response from url using volley

嗨,我想迭代一个看起来像这样的json字符串:

{
  "vendor":[ 
             {
               "vendor_name":"Tapan Moharana",
               "vendor_description":"",
               "vendor_slug":"tapan",
               "vendor_logo":null,
               "contact_number":null
             }
           ],
           "products":
              {
                "25": 
                  {
                    "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"
                  },
                "26":
                  {
                    "name":"Chicken Chilly",
                    "price":"234.0000",
                    "image":"http:\/\/carrottech.com\/lcart\/media\/catalog\/product\/cache\/1\/image\/150x\/9df78eab33525d08d6e5fb8d27136e95\/c\/h\/cheicken.jpg"
                  },
                "27":
                 {
                    "name":"Chicken Biryani",
                    "price":"500.0000",
                    "image":"http:\/\/carrottech.com\/lcart\/media\/catalog\/product\/cache\/1\/image\/150x\/9df78eab33525d08d6e5fb8d27136e95\/placeholder\/default\/image_1.jpg"
                  }
              }
   }

这里是json字符串的更好视图:

在此输入图像描述

我正在使用以下代码遍历此json字符串的供应商数组:

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

但我不知道如何从“产品”对象获取数据请帮助我如何设置我的json对象迭代“产品”

当我尝试更改阵列的格式,以便产品和供应商都是一个单独的json数组时,我仍然得到上面的json格式..

这就是我在做的事情

$resp_array['vendor'] = $info;
$resp_array['products'] = $vendorProductsInfo;
$resp_array = json_encode($resp_array);
    print_r($resp_array);

请在这件事上给予我帮助

修改后的问题:

我修改了我的网络响应,如下所示:

[{"entity_id":24,"product_name":"Burger","product_image_url":"\/b\/u\/burger_large.jpg","price":"234.0000","category_id":59},{"entity_id":27,"product_name":"Chicken Biryani","product_image_url":"\/b\/i\/biryani.jpg","price":"500.0000","category_id":59},{"entity_id":31,"product_name":"Pizza","product_image_url":"\/p\/i\/pizza_png7143_1.png","price":"125.0000","category_id":59}]

和代码:

 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") + "<----");
                        }
                        JSONObject jsono1 = new JSONObject(response);
                        JSONArray childrenProducts = jsono1.getJSONArray("products");
                        for(int i=0;i<childrenProducts.length();i++){
                            JSONObject jsonData = childrenProducts.getJSONObject(i);
                            System.out.print(jsonData.getString("name") + "<----dd");
                        }

但仍然产品部分不起作用...请帮助

这是工作解决方案:使用GOOGLE GSON(开源jar)

import java.io.IOException;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;



     public class JsonToJava {

            public static void main(String[] args) throws IOException {
                try{
                    String json = "<YOUR_JSON>";
                    Gson gson = new GsonBuilder().create();
                    VendorInfo vInfo = gson.fromJson(json, VendorInfo.class);       
                    System.out.println(vInfo.getVendorName());              
                } catch(Exception ex) {
                    ex.printStackTrace();
                }
            }
        }

为供应商和产品创建类

public class Vendor {
    public String vendor_name;
    public String vendor_description;
    public String vendor_slug;
    public String vendor_logo;
    public String contact_number;

    public String getName() {
        return vendor_name;
    }
}

public class Product {
    public String name;
    public long price;
    public String image;

    public String getName() {
        return name;
    }
}

VendorInfo是JSON对象形式:

import java.util.Map;

public class VendorInfo {
    public Vendor[] vendor;
    public Map<Integer, Product> products;

    public String getVendorName() {
        return vendor[0].getName();
    }
    public Product getProduct() {
        System.out.println(products.size());
        return products.get(25);
    }
}

您可以为Vendor,Product和VendorInfo添加getter。 你完成了! 您将获得所有数据。

JsonToJava的输出:

Tapan Moharana

要获取产品数据,您需要使用Iterator

   JSONObject jProducts = jsonObject
            .optJSONObject("products");
    try {
        if (jProducts
                .length() > 0) {
            Iterator<String> p_keys = jProducts
                    .keys();
            while (p_keys
                    .hasNext()) {
                String keyProduct = p_keys
                        .next();
                JSONObject jP = jProducts
                        .optJSONObject(keyProduct);

                if (jP != null) {
                    Log.e("Products",
                            jP.toString());
                }
            }
        }
    } catch (Exception e) { // TODO:
        // handle
        // exception
    }

你可以试试这个

JSONObject jsono = null;
    try {
        jsono = new JSONObject(response);
        JSONObject productObject = jsono.getJSONObject("products");
        Iterator<String> keys = productObject.keys();

        while (keys.hasNext())
        {
            // get the key
            String key = keys.next();

            // get the value
            JSONObject value = productObject.getJSONObject(key);

            //get seprate objects
            String name = value.getString("name");
            String image = value.getString("image");
            Log.i(TAG,name+"-"+image);

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

试试这个 :

JSONObject productObject = jsono.getJSONObject("products");

JSONObject json_25 = productObject getJSONObject("25");
String name_25= json_25.getString("name");
String price_25= json_25.getString("price");
String image_25= json_25.getString("image");

JSONObject json_26 = productObject getJSONObject("26");
String name_26= json_26.getString("name");
String price_26= json_26.getString("price");
String image_26= json_26.getString("image");

JSONObject json_27 = productObject getJSONObject("27");
String name_27= json_27.getString("name");
String price_27= json_27.getString("price");
String image_27= json_27.getString("image");

暂无
暂无

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

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