我有问题从android改造中的键值中删除空格

问题描述 投票:0回答:1

我试图通过json发布值进行改造,但遗憾的是我得到了json格式的空间,以便我无法从android中的改造中获得成功响应。

Retrofit retrofit = new Retrofit.Builder()
                        .baseUrl(activity.getResources().getString(R.string.serverurl))
                        .addConverterFactory(GsonConverterFactory.create())
                        .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                        .build();

                InthreeApi apiService = retrofit.create(InthreeApi.class);
                JSONObject paramObject = null; // Main JSON Object
                JSONObject jsonFTcustomer = null;
                JSONArray jsonDetailsArray = new JSONArray();

                //String orderId=orderid.trim();
                String orderId = orderid.replaceAll("\\s", "");

                try {

                    paramObject=new JSONObject();
                    //paramObject.put("order_id",orderId.trim().replace("\\s", ""));


                    paramObject.put("order_id","orderid");


                    for(int i=0;i<productItems.size();i++) {
                        JSONObject jsonObject=new JSONObject();
                        jsonObject.put("product_id", productItems.get(i).getProductId());
                        jsonObject.put("product_name", productItems.get(i).getProductName());
                        jsonObject.put("sku", productItems.get(i).getProductCode());
                        jsonObject.put("qty", productItems.get(i).getQty());
                        jsonDetailsArray.put(jsonObject);
                    }

                    paramObject.put("products",jsonDetailsArray);
                    Log.v("cancle_order",String.valueOf(paramObject));




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

                   // jsonFTcustomer.toString().replaceAll("^\"|\"$", "");

                RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), paramObject.toString());

Getting JSON:

{"order_id":" 000753398","products":[{"product_id":"16869","product_name":" Minix 4000mah Power Bank","sku":"116869","qty":"1"}]}

Expected JSON:

{"order_id":"000753398","products":[{"product_id":"16869","product_name":" Minix 4000mah Power Bank","sku":"116869","qty":"1"}]}

我需要删除空间order_id键值,但我找不到解决方案。预先感谢。

android json retrofit retrofit2 android-json
1个回答
0
投票

你的for循环如下所示,请检查它。

for(int i=0;i<productItems.size();i++) {

      JSONObject jsonObject=new JSONObject();
      jsonObject.put("product_id", productItems.get(i).getProductId().trim());
      jsonObject.put("product_name", productItems.get(i).getProductName().trim());
      jsonObject.put("sku", productItems.get(i).getProductCode().trim());
      jsonObject.put("qty", productItems.get(i).getQty().trim());
      jsonDetailsArray.put(jsonObject);
}

trim()使用此功能删除空格。

更新

String orderId = orderid.replaceAll(" ", "");
paramObject.put("order_id",orderId);
© www.soinside.com 2019 - 2024. All rights reserved.