2015-04-24 51 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