2017-08-15 56 views
-1

我想從PHP獲取一個數組,其中一個fileds是一個數組。 如何用jsonArray和jsonObject獲取籃子的數據? (在下面的代碼中,籃子是一個包含5個參數的數組)。如何在android中使用兩個數組獲取JSON數據

這是我的數組:

[{"orderCode":11514,"orderDate":"2017/05/21","orderPrice":"1‌​9200","fullName":"Ja‌​ck","address":"addr 1","cellphone":"09151515730","basket":[{"b_qty":"4","pid":"8‌​","b_price":"9500","‌​b_discount":"10","ti‌​tle":"obj1"}] 

編輯

String b_price =""; 
    String b_discount=""; 
    String b_qty =""; 
    String ti‌​tle= ""; 
    String pid=""; 

    try { 
     JSONArray jsonArray = new JSONArray(data); 

     for (int i = 0; i < jsonArray.length(); i++) { 

      JSONObject object = jsonArray.getJSONObject(i); 

      String orderCode = object.getString("orderCode"); 
      String orderDate = object.getString("orderDate"); 
      String orderPrice = object.getString("orderPrice"); 
      String fullName = object.getString("fullName"); 
      String address = object.getString("address"); 
      String cellphone = object.getString("cellphone"); 

      JSONArray orderBasket = object.getJSONArray("basket"); 

      for (int j = 0; j < orderBasket.length(); j++){ 
       JSONObject object1 = orderBasket.getJSONObject(j); 

       b_qty = object1.getString("b_qty"); 
       pid = object1.getString("pid"); 
       b_price = object1.getString("b_price"); 
       b_discount = object1.getString("b_discount"); 
       ti‌​tle = object1.getString("ti‌​tle"); 

      } 

      CustomOrderList customOrderList = new CustomOrderList(getApplicationContext()); 
      customOrderList.orderCode.setText(orderCode); 
      customOrderList.orderDate.setText(orderDate); 
      customOrderList.orderTotalPrice.setText(orderPrice); 
      customOrderList.orderFullName.setText(fullName); 
      customOrderList.orderAddress.setText(address); 
      customOrderList.orderCellphone.setText(cellphone); 

      customOrderList.basketPrice.setText(b_price); 
      customOrderList.basketDiscount.setText(b_discount); 
      customOrderList.basketTitle.setText(ti‌​tle); 

      layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); 
      linearOrders.addView(customOrderList); 
     } 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
+0

從添加您的JSON和解釋越多,你想要做 –

+0

我看你把 「菜籃子」 作爲字符串什麼這裏。我不完全明白你想達到什麼目的,這裏的代碼不太相關,發佈你正在處理的JSON結構!然後告訴我們你想提取什麼 – Mercury

+0

我想在我的應用程序的其他細節下顯示籃子的數據。]我用這段代碼正確獲取其他數據,但是我無法使用此代碼獲取籃子的數據。 – Saeidhp

回答

-1
for (int i = 0; i < jsonArray.length(); i++) { 
     JSONObject object = jsonArray.getJSONObject(i); 

     String orderCode = object.getString("orderCode"); 
     String orderDate = object.getString("orderDate"); 
     String orderPrice = object.getString("orderPrice"); 
     String fullName = object.getString("fullName"); 
     String address = object.getString("address"); 
     String cellphone = object.getString("cellphone"); 

//Now orderBasket is jsonArray 
     JSONArray orderBasket = object.getJSONArray("basket"); 

// So fetch all objects from orderBasket array 
     for (int j = 0; j < orderBasket.length(); j++){ 
     JSONObject objectbsk = orderBasket.getJSONObject(j); 

     String b_qty = objectbsk.getString("b_qty"); 
     String pid = objectbsk.getString("pid"); 
     String b_price = objectbsk.getString("b_price"); 
     String b_discount = objectbsk.getString("b_discount"); 
     String ti‌​tle = objectbsk.getString("ti‌​tle"); 
// Create a orderBasket list for Basket with these keys also as you have created for CustomOrderList and use it 
} 

     CustomOrderList customOrderList = new CustomOrderList(getApplicationContext()); 
     customOrderList.orderCode.setText(orderCode); 
     customOrderList.orderDate.setText(orderDate); 
     customOrderList.orderTotalPrice.setText(orderPrice); 
     customOrderList.orderFullName.setText(fullName); 
     customOrderList.orderAddress.setText(address); 
     customOrderList.orderCellphone.setText(cellphone); 
    customOrderList.orderBasket.setText(orderBasket); 

// here you can't do set text since it's jsonarray. So use basketlist to show the data` 

layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); 
      linearOrders.addView(customOrderList); 
     } 
+0

你可以檢查你的代碼嗎?此代碼有錯誤customOrderList.orderBasket.setText(orderBasket);並且我在這部分中將i更改爲j(int j = 0; i Saeidhp

+0

Yaah我已編輯,我到j。你說的第一行是我在我的回答中寫道,你不能設置一個jsonarray(orderbasket)。因此,創建一個listoforderbasket ...請閱讀我已編輯的答案。希望它有幫助 –

+0

對不起,重複該問題。我不得不編輯我的問題像你的代碼,但它不工作too.Please閱讀我編輯的問題。 – Saeidhp

相關問題