jsonBody 数组只有一个索引

jsonBody array only one index

ArrayList<String> products = new ArrayList<>();

    RequestQueue queue = Volley.newRequestQueue(PaymentActivity.this);
    String url = backendUrl+"/api/orders/createorder";

    for(PModel pModel: pModels){
        products.add(pModel.toString());
    }

    Log.e(TAG, "createOrder: products array size"+products.size());

    JSONObject jsonBody = new JSONObject();
    try {
        jsonBody.put("products", products);
        jsonBody.put("totalprice", convertFloat((float) totalCostDouble));
        jsonBody.put("productsprice", convertFloat((float) productCostDouble));
        jsonBody.put("shippingprice", convertFloat((float) shippingCostInt));
        jsonBody.put("tax", convertFloat((float) TAX));

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

    JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
            url, jsonBody,
            response -> {
                Log.e(TAG, "createOrder: response"+response);
            }, error -> {alertDialog.dismiss();
        VolleyLog.e("JSONPost", "Error: " + error.getMessage());
    });

    jsonObjReq.setRetryPolicy(new DefaultRetryPolicy(
            0,
            DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
            DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));


    queue.add(jsonObjReq);

我正在使用 volley 将 post 数据输入我的后端,问题是我有一个 "Products" 数组,但是当我检查我的 mongo 数据库时,我只得到一个索引它包含所有产品,即使我在 Log

中获得尺寸 3

试着像 ArrayList 那样投射:

jsonBody.put("products", (Object) products);

或类似的:

jsonBody.put("products", new JSONArray(products));

尝试

JSONArray productArray = new JSONArray(products);
jsonBody.put("products", productArray.toString());