2016-05-20 160 views
3

你好我試圖通過一個JSON字符串,看起來像這樣來遍歷JSON數組使用JSON從URL響應後追加JSON字符串中的Android迭代:使用凌空

{ 
    "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字符串:

enter image description here

我使用這個代碼通過該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"); 
         } 

,但仍是部分產品無法正常工作......請幫助

+0

嗨。如果您認爲我提供的解決方案是正確的,請將其標記爲完整。謝謝。 –

回答

0

可以試試這個

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

我正在測試它..PLZ給我一分鐘 –

+0

它沒有工作..它正在考慮productObject作爲一個整體字符串不是它的每個部分作爲對象.. JSONObject productChildren = jsono.getJSONObject(「products」);對於(int i = 0; i

+0

@ShahidSarwar請顯示我的更新回答 –

-1

試試這個:

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

Bro這是一個靜態實現,我需要使用while循環或for循環,因爲從url獲取的數據不會總是具有相同的大小。 –

+0

但你得到的迴應格式錯誤。而不是25,26,27應該有json數組 – Shubham

+0

是的,我試圖把它放在我的webservice數組中 –

1

爲了讓您的產品數據,您需要使用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 
    } 
2

這裏是工作的解決方案:使用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); 
    } 
} 

您可以添加您爲干將供應商,產品和VENDORINFO。你完成了!你會得到所有的數據。

輸出JsonToJava的:

Tapan Moharana