Java 如何从JSON数组中获取列的和

Java 如何从JSON数组中获取列的和,java,arrays,json,Java,Arrays,Json,这在我的Response.Listener中,在我的StringRequest中 try { JSONObject jsonObject = new JSONObject(response); JSONArray array = jsonObject.getJSONArray("cart"); for (int i = 0

这在我的Response.Listener中,在我的StringRequest中

                try {
                        JSONObject jsonObject = new JSONObject(response);
                        JSONArray array = jsonObject.getJSONArray("cart");
                        for (int i = 0; i<array.length(); i++){
                            JSONObject o = array.getJSONObject(i);
                            CartItem item = new CartItem(
                                    o.getString("cardno"),
                                    o.getString("product_id"),
                                    o.getString("name"),
                                    o.getString("quantity"),
                                    o.getString("price"),
                                    o.getString("category")
                            );
                            cartItems.add(item);
                        }
                        adapter = new CartAdaptor(cartItems, getContext());
                        recyclerView.setAdapter(adapter);

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

我如何计算该数组中的所有价格?

我不确定您想从哪里获得总价

我建议一个

            try {
                    int total = 0;                         //  add this

                    JSONObject jsonObject = new JSONObject(response);
                    JSONArray array = jsonObject.getJSONArray("cart");
                    for (int i = 0; i<array.length(); i++){
                        JSONObject o = array.getJSONObject(i);
                        CartItem item = new CartItem(
                                o.getString("cardno"),
                                o.getString("product_id"),
                                o.getString("name"),
                                o.getString("quantity"),
                                o.getString("price"),
                                o.getString("category")
                        );
                        cartItems.add(item);

                        //  add this
                        if(o.getString("price") != null &&
                           o.getString("price") != "" ){
                            total += Integer.parseInt(o.getString("price"));
                        }


                    }
                    adapter = new CartAdaptor(cartItems, getContext());
                    recyclerView.setAdapter(adapter);

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
试试看{
int total=0;//添加此
JSONObject JSONObject=新JSONObject(响应);
JSONArray数组=jsonObject.getJSONArray(“cart”);

对于(int i=0;i)您确切的问题是什么?您不知道如何循环数组?您不知道如何访问
CarItem
中的成员变量?您不知道如何将字符串转换为数字?您不知道如何添加数字?
            try {
                    int total = 0;                         //  add this

                    JSONObject jsonObject = new JSONObject(response);
                    JSONArray array = jsonObject.getJSONArray("cart");
                    for (int i = 0; i<array.length(); i++){
                        JSONObject o = array.getJSONObject(i);
                        CartItem item = new CartItem(
                                o.getString("cardno"),
                                o.getString("product_id"),
                                o.getString("name"),
                                o.getString("quantity"),
                                o.getString("price"),
                                o.getString("category")
                        );
                        cartItems.add(item);

                        //  add this
                        if(o.getString("price") != null &&
                           o.getString("price") != "" ){
                            total += Integer.parseInt(o.getString("price"));
                        }


                    }
                    adapter = new CartAdaptor(cartItems, getContext());
                    recyclerView.setAdapter(adapter);

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