2015-04-24 83 views
0

以下是我對服務器的迴應,我能夠得到clientproduct數組,但無法得到bulkprice數組及其json對象。我不知道問題是什麼。如何訪問JSON數組內部的JSON數組?

{"status":"success","clientproduct":[{"pid":"4","name":"kangan pair","unitprice":"1500","boxqty":"1","bulkprice":[{"minqty":"10","price":"1500"},{"minqty":"15","price":"1470"},{"minqty":"20","price":"1460"}]}]} 

代碼

if(bxqtyy.getText().toString().trim().length() > minimum_qty.toString().equals()) 
          { 
           System.out.println("NKDBKDKB"); 
          } 

代碼在這裏

if (json != null) { 
        try { 
         JSONObject jsonObj = new JSONObject(json.toString()); 

         // Getting JSON Array node 
         clientproduct = jsonObj.getJSONArray(CLIENTPRODUCT_LIST); 

         // looping through All Contacts 
         for (int i = 0; i < clientproduct.length(); i++) { 
          ck = clientproduct.getJSONObject(i); 

          unitp=ck.getString("unitprice"); 
          System.out.println("Unit ni price"+unitp); 

          boxq=ck.getString("boxqty"); 
          System.out.println("Box ni quantity "+boxq); 

          // creating new HashMap 
          /*HashMap<String, String> map = new HashMap<String, String>(); 

          // adding each child node to HashMap key => value 
          map.put(UNIT_PRICE, c.getString(UNIT_PRICE)); 
          map.put(BOX_QTY,c.getString(BOX_QTY)); 
          clientproductdata.add(map);*/ 

          if(clientproduct !=null) 
          { 
           bulkprice = ck.getJSONArray(BULKPRICE_LIST); 



           for (int a = 0; a < bulkprice.length(); a++) { 
            jo = bulkprice.getJSONObject(a); 


            /* if(c.getString("boxqty").toString().equals("") == jo.getString("minqty").toString().equals("15")) 
            { 

            }*/ 
            minimum_qty=jo.getString("minqty"); 
            System.out.println("MinQuantitiy"+minimum_qty); 

            pricess=jo.getString("price"); 
            System.out.println("Box price "+pricess); 
            System.out.println("Bularay"+bulkprice); 
           } 

          } 
          else 
          { 
           System.out.println("Badluck"); 
          } 

         } 

        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 
       } else { 
        Log.e("ServiceHandler", "Couldn't get any data from the url"); 
       } 

      runOnUiThread(new Runnable() 
      { 
       @Override 
       public void run() 
       { 
        //Toast.makeText(Add_ClientOrder.this, msg, Toast.LENGTH_LONG).show(); 
       } 
      }); 
      return json.getString(FEEDBACK_SUCCESS); 

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

    // After completing background task Dismiss the progress dialog 

    protected void onPostExecute(String file_url) { 
     //dismiss the dialog once product deleted 
     pDialog.dismiss(); 
     autoproduct.setOnItemClickListener(new OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> parent, View view, 
        int position, long id) { 
       // TODO Auto-generated method stub 
       uprice.setText(unitp); 
       bxqtyy.setText(boxq); 

       bxqtyy.addTextChangedListener(new TextWatcher() { 

        @Override 
        public void onTextChanged(CharSequence s, int start, int before, int count) { 
         if(bxqtyy.getText().toString().trim().length() > minimum_qty.toString().equals("15")) 
          { 
           System.out.println("NKDBKDKB"); 
          } 
        } 

        @Override 
        public void beforeTextChanged(CharSequence s, int start, int count, 
          int after) { 
         // TODO Auto-generated method stub 

        } 

        @Override 
        public void afterTextChanged(Editable s) { 
         // TODO Auto-generated method stub 

        } 
       }); 

      } 
     }); 


     //parentcat.getText().clear(); 
}} 
+0

代替'bulkprice = jsonObj.getJSONArray(BULKPRICE_LIST);' 你應該寫'bulkprice = c.getJSONArray(BULKPRICE_LIST);' –

回答

1

我能夠得到clientproduct數組,但沒能獲得陣列 bulkprice陣列

bulkprice JSONArray在JSONObject之內clientproduct JSONArray。所以使用c而不是jsonObj得到bulkprice JSONArray:

bulkprice = c.getJSONArray(BULKPRICE_LIST); 
+0

是它的工作..感謝 – chris

+0

@chris:哪個問題? –

+0

@克里斯:股問題的URL –

0

這是我的決定:

JSONArray big = jsonObject.getJSONArray("clientproduct"); 
for(int i = 0; i < big.lenght(); i++) { 
    JSONArray little = big.getJSONObject(i).getJSONArray("bulkprice"); 
} 
+0

我越來越bulkarray完美,,這裏默認情況下,我得到boxqty 1,現在如果用戶在14的EditText然後輸入價格應保持1500,如果用戶輸入15,應該得到1470,但我沒能得到滿足,這與這個問題,它不允許我做這樣 – chris

0

使用這些代碼...它可以對你有所幫助....

JSONArray outer_json_array = RESPONSE.getJSONArray("clientproduct"); 

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

    JSONArray inner_json_array = outer_json_array.getJSONObject(i).getJSONArray("bulkprice"); 

    for(int j=0;j<inner_json_array.length();j++) { 
    } 
} 
+0

可以helpppp – chris

+0

@克里斯發生了什麼事? –

+0

http://stackoverflow.com/questions/41217237/viewpager-not-getting-last-item – chris