JsonArray循环取数据

JsonArray中 有4条数据 (jsonDecorationId---------:[{"decorationId":4},{"decorationId":6},{"decorationId":16},{"decorationId":24}]),怎么把这4条数据循环放入到 查询条件中

img

ArrayList<CustomerTankDecoration> customerTankDecorationId = tankMapper.selectDecorationId(((JSONObject) o).getIntValue("id"));
                JSONArray jsonDecorationId = JSONArray.parseArray(JSON.toJSONString(customerTankDecorationId));
                for (Object c : jsonDecorationId) {
                    ArrayList<TankDecoration> tankDecoration =
                            tankMapper.selectCustomerTankDecoration(BigInteger.valueOf(((JSONObject) c).getIntValue("decorationId")));
                    JSONArray jsonDecoration = JSONArray.parseArray(JSON.toJSONString(tankDecoration));
                    for (Object b : jsonDecoration) {
                        JSONObject jsonObjectDecoration = new JSONObject(new LinkedHashMap<>());
                        JSONArray jsonArrayDecoration = new JSONArray();
                        jsonObjectDecoration.put("code",((JSONObject) b).getBigInteger("code"));
                        jsonObjectDecoration.put("category",((JSONObject) b).getBigInteger("category"));
                        jsonArrayDecoration.add(jsonObjectDecoration);
                        json.put("decorations",jsonArrayDecoration);
                        System.out.println("jsonDecoration---------:" + jsonDecoration);
                        System.out.println("jsonObjectDecoration---------:" + jsonObjectDecoration);
                        System.out.println("json---------:" + json);
                        System.out.println("jsonDecorationId---------:" + jsonDecorationId);
                        System.out.println("jsonArrayDecoration---------:" + jsonArrayDecoration);
                    }
                }

根据你提供的代码,你可能需要将 jsonDecoration 中的数据循环添加到一个 JSONArray 中,并在循环结束后将这个 JSONArray 添加到 json 中作为一个名为 decorations 的字段。具体代码可以参考下面的示例:

ArrayList<CustomerTankDecoration> customerTankDecorationId = tankMapper.selectDecorationId(((JSONObject) o).getIntValue("id"));
JSONArray jsonDecorationId = JSONArray.parseArray(JSON.toJSONString(customerTankDecorationId));
JSONArray jsonArrayDecoration = new JSONArray();
for (Object c : jsonDecorationId) {
    ArrayList<TankDecoration> tankDecoration =
        tankMapper.selectCustomerTankDecoration(BigInteger.valueOf(((JSONObject) c).getIntValue("decorationId")));
    JSONArray jsonDecoration = JSONArray.parseArray(JSON.toJSONString(tankDecoration));
    for (Object b : jsonDecoration) {
        JSONObject jsonObjectDecoration = new JSONObject(new LinkedHashMap<>());
        jsonObjectDecoration.put("code",((JSONObject) b).getBigInteger("code"));
        jsonObjectDecoration.put("category",((JSONObject) b).getBigInteger("category"));
        jsonArrayDecoration.add(jsonObjectDecoration);
    }
}

// 将 jsonArrayDecoration 添加到 json 中
json.put("decorations", jsonArrayDecoration);

// 输出相关信息做调试
System.out.println("jsonDecorationId---------:" + jsonDecorationId);
System.out.println("jsonArrayDecoration---------:" + jsonArrayDecoration);
System.out.println("json---------:" + json);

代码中将循环遍历 jsonDecoration 中的数据,构造成一个新的 JSONArray 对象,最后将这个 JSONArray 对象添加到 json 中作为 decorations 字段。需要注意的是,添加 decorations 字段的代码应该放到整个 for 循环之外,这样才能保证 decorations 字段只添加一次。

可以使用for循环遍历JsonArray中的每一个JsonObject,将其取出的decorationId作为查询条件,然后将查询到的结果放入一个新的JsonArray中。具体实现如下:

JSONArray jsonDecorationId = ... // 从数据库中查询到的JsonArray
JSONArray decorations = new JSONArray(); // 用于存放查询到的结果

for (Object o : jsonDecorationId) {
    int decorationId = ((JSONObject) o).getIntValue("decorationId");
    List<TankDecoration> decorationList = tankMapper.selectCustomerTankDecoration(BigInteger.valueOf(decorationId));
    for (TankDecoration decoration : decorationList) {
        JSONObject decorationJson = new JSONObject();
        decorationJson.put("code", decoration.getCode());
        decorationJson.put("category", decoration.getCategory());
        decorations.add(decorationJson);
    }
}

// 将结果添加到原有的Json对象中
json.put("decorations", decorations);

在上述代码中,我们首先遍历了jsonDecorationId中的每一个JsonObject,取出其中的decorationId作为查询条件,查询出对应的TankDecoration对象,并将其转换成JsonObject格式,然后将所有查询到的JsonObject添加到一个新的JsonArray中。最后,我们将这个新的JsonArray添加到原有的Json对象中,以便后续使用。

  • 你可以参考下这个问题的回答, 看看是否对你有帮助, 链接: https://ask.csdn.net/questions/831670
  • 我还给你找了一篇非常好的博客,你可以看看是否有帮助,链接:jsonArray遍历 取出数组中对象
  • 除此之外, 这篇博客: JSONArray数据到前端双引号被转义中的 解决方法 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
    1. 用ajax传值就不会出现上面的这两个问题
    @PostMapping("/findAllTips")
    @ResponseBody
    public JSONArray findAllTips() {
        return blogTipService.findAllBlogTipToJson();
    }
    
    $.ajax({
            type: "post",
            url: "/findAllTips",
            success: function (data) {
                alert(parseJSON[0].name);
            }
        });
    

    在这里插入图片描述

    1. 也可以后端传字符串,然后前端将字符串转成json数组
    @PostMapping("/findAllTips")
    @ResponseBody
    public String findAllTips() {
        return blogTipService.findAllBlogTipToJson().toJSONString();
    }
    
    $.ajax({
            type: "post",
            url: "/findAllTips",
            success: function (data) {
                var parseJSON = $.parseJSON(data);
                alert(parseJSON[0].name);
            }
        });
    

    在这里插入图片描述

  • 以下回答由chatgpt基于相关博客总结生成:

    使用Java的JSONArray和JSONObject类来实现循环遍历一个JsonArray的操作,示例代码如下:

    JSONArray jsonArray = new JSONArray(); for (int i = 0; i < jsonArray.length(); i++) { JSONObject jsonObject = jsonArray.getJSONObject(i); // Do something with jsonObject here }

    在循环中,可以通过getJSONObject()方法来获取JsonArray中的每一个JSONObject对象,然后根据需要进行处理。