【问题标题】:Iterating through json array with appended json string in android after json as response from url using volley使用 volley 作为来自 url 的响应,在 json 之后在 android 中使用附加的 json 字符串遍历 json 数组
【发布时间】:2016-05-20 06:33:56
【问题描述】:

您好,我正在尝试遍历一个看起来像这样的 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");
                        }

但产品部分仍然无法正常工作...请帮助

【问题讨论】:

  • 嗨。如果您觉得我提供的解决方案是正确的,请将其标记为完成。谢谢。

标签: java android json android-volley android-json


【解决方案1】:

这是可行的解决方案:使用 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

【讨论】:

    【解决方案2】:

    要获取您的产品数据,您需要使用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
        }
    

    【讨论】:

      【解决方案3】:

      你可以试试这个

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

      【讨论】:

      • 它不起作用..它正在将productObject视为一个完整的字符串,而不是它的每个部分作为对象.. JSONObject productChildren = jsono.getJSONObject("products"); for(int i=0;i
      【解决方案4】:

      试试这个:

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

      【讨论】:

      • 兄弟,这是一个静态实现,我需要使用 while 或 for 循环对其进行迭代,因为从 url 获取的数据并不总是具有相同的大小。
      • 但是您得到的响应格式错误。而不是 25,26,27 应该有 json 数组
      • 是的,我正在尝试将它放入我的网络服务中的数组中
      • 如果它在数组中,那么您可以解析供应商等项目。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多