【发布时间】:2019-09-24 20:00:03
【问题描述】:
无法根据响应在 JSONObject 中设置数组。下面是我无法在 jsonobject 中设置数组的代码。如何在我的 jsonobject 中发送数组的键值,该数组共享了从 postman 获取的代码的响应
这是代码中的正确方法吗
代码--
JsonArray array = new JsonArray();
array.add(productId);
array.add(qty);
JSONObject jsonObject = new JSONObject();
jsonObject.put("productDetails", array);**
这是 MainActivity 中的代码。问题是在我的 JSON 对象中没有得到正确的 jsonarray,所以 API 不会正确命中 这些 String 键值用于传入请求参数
String key="WSEoaGBifOEIS5dd6vQ5tfbs3R1c8Rsz";
String affId="teamfotog";
String act="photoStores";
String latitude="40.7127753";
String longitude="-74.0059728";
String devinf="Android,7.0";
String appver="1.00";
String productId="6670002";
String qty="3";
//productDetails
**JsonArray array = new JsonArray();
array.add(productId);
array.add(qty);**
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("apiKey", key);
jsonObject.put("affId", affId);
jsonObject.put("act", act);
jsonObject.put("latitude", latitude);
jsonObject.put("longitude", longitude);
jsonObject.put("devinf", devinf);
jsonObject.put("appver", appver);
**jsonObject.put("productDetails", array);**
JsonParser jsonParser = new JsonParser();
ApiStorePhotoInterface apiInterface = ApiStorePhotoClient.getApi();
Call<PhotoStoreMainModel> call = apiInterface.getResponse((JsonObject) jsonParser.parse(jsonObject.toString().trim()));
请求参数在 Jsonbody 中 --
{"apiKey":"WSEoaGBifOEIS5dd6vQ5tfbs3R1c8Rsz","affId":"teamfotog","act":"photoStores","latitude":"40.7127753","longitude":"-74.0059728","devinf":"Android,7.0","appver":"1.00","productDetails":[{"productId":"6670002","qty":"3"}]}
【问题讨论】: