以下是我對服務器的迴應,我能夠得到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();
}}
代替'bulkprice = jsonObj.getJSONArray(BULKPRICE_LIST);' 你應該寫'bulkprice = c.getJSONArray(BULKPRICE_LIST);' –