【问题标题】:How to set array in jsonobject如何在jsonobject中设置数组
【发布时间】: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"}]}

【问题讨论】:

    标签: java android json gson


    【解决方案1】:

    当然,这是行不通的。您直接在JsonArray 中添加对象(Strings)。在响应正文中,您真正想要的是JsonArray 内的JsonObject。试试这个 -

    JsonObject productDetail = new JsonObject();
    productDetail.addProperty("productId", productId);
    productDetail.addProperty("qty", qty);
    
    JsonArray array = new JsonArray();
    array.add(productDetail);
    

    【讨论】:

    • 所以我必须为此创建新的 jsonobject ?
    • JSONArray 不工作,但 JsonArray 正在处理空响应
    • 从响应体中可以看出是一个对象数组。
    • { “apiKey”:“WSEoaGBifOEIS5dd6vQ5tfbs3R1c8Rsz”,“affId”:“teamfotog”,“act”:“photoStores”,“纬度”:“40.7127753”,“经度”:“-74.0059728”, "devinf": "Android,7.0", "appver": "1.00", "productDetails": "[\"{\\\"productId\\\":\\\"6670002\\\",\\\ "qty\\\":\\\"3\\\"}\"]" } 从调试中得到这个
    【解决方案2】:

    试试这个。

    jsonObject.put("productDetails",(Object)array);
    

    【讨论】:

    • 从调试器获取这个请求---
    • { “apiKey”:“WSEoaGBifOEIS5dd6vQ5tfbs3R1c8Rsz”,“affId”:“teamfotog”,“act”:“photoStores”,“纬度”:“40.7127753”,“经度”:“-74.0059728”, "devinf": "Android,7.0", "appver": "1.00", "productDetails": "[\"{\\\"productId\\\":\\\"6670002\\\",\\\ "数量\\\":\\\"3\\\"}\"]" }
    • 预期 --{ “apiKey”:“WSEoaGBifOEIS5dd6vQ5tfbs3R1c8Rsz”,“affId”:“teamfotog”,“act”:“photoStores”,“纬度”:“40.7127753”,“经度”:“- 74.0059728", "devinf": "Android,7.0", "appver": "1.00", "productDetails": [{ "productId": "6670002", "qty": "3" }] }
    【解决方案3】:

    如果有人仍然有这个问题,这就是为我解决的问题:

    用途:

    JSONArray
    

    代替

    JsonArray
    

    【讨论】:

      【解决方案4】:

      你应该把产品细节模型放到数组中

                  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";     
      
                  JsonObject product = new JsonObject();
                  product.put("productId",productId);
                  product.put("qty",qty);
      
      
                  JsonArray array = new JsonArray();
                  array.add(product);
      
      
                  JSONObject jsonObject = new JSONObject();
                  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);
      

      【讨论】:

        猜你喜欢
        • 2021-06-28
        • 2016-10-26
        • 2019-05-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-09-22
        相关资源
        最近更新 更多