如何在JSON数组中访问JSON数组?

时间:2015-04-24 06:35:59

标签: android json

以下是我对服务器的响应,我能够获得clientproduct数组,但无法得到bulkprice数组及其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();
}}

3 个答案:

答案 0 :(得分:1)

  

我能够获得clientproduct数组但无法获取数组   bulkprice数组

bulkprice JSONArray位于JSONObject JSONArray的clientproduct内。所以使用c代替jsonObj来获取bulkprice JSONArray:

bulkprice = c.getJSONArray(BULKPRICE_LIST);

答案 1 :(得分:0)

这是我的决定:

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

答案 2 :(得分: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++) {
    }
}