我正在实现Java SpringBoot以制作RestAPI应用程序。我坚持下面的循环。该问题似乎与sub1_arr.add(subResult1);有关。

我进行了一些测试:

  • 为每次迭代打印SubResult1:println显示正确的元素,但是当添加到sub1_arr时,它将重复。
  • 测试1(如代码所示):这是我编写的初始代码。输出不正确,输出重复
  • 测试2(如代码所示):具有正确的输出,但为字符串形式。
  •     while(rs.next())
        {
            if(rs.getString("status").equals("FAILED")) {
                obj.put("result", "failed");
                obj.put("msg", rs.getString("msg"));
            }
            else {
    
                //mainResult - ignore for a while
                mainResult.put("layer1_id", rs.getString("layer1_id"));
                mainResult.put("layer1_name", rs.getString("layer1_name"));
    
                //subResult - layer2
                subResult1.put("layer2_id", rs.getString("layer2_id"));
                subResult1.put("layer2_name", rs.getString("layer2_name"));
    
                //test 1 - having incorrect output with all the element become duplicate
                sub1_arr.add(subResult1);
    
                //test 2 - having correct output but the output become string with \"xxx\"
                sub1_arr.add(subResult1.toString());
            }
    
        }
    
        //adding sub array to main result
        mainResult.set("layer2", sub1_arr);
    

    获取输出(意外)
    {"layer2":[
       {"layer2_id" : "3", "layer2_name" : "name_3"},
       {"layer2_id" : "3", "layer2_name" : "name_3"},
       {"layer2_id" : "3", "layer2_name" : "name_3"}
    ]}
    

    预期产量
    {"layer2":[
       {"layer2_id" : "1", "layer2_name" : "name_1"},
       {"layer2_id" : "2", "layer2_name" : "name_2"},
       {"layer2_id" : "3", "layer2_name" : "name_3"}
    ]}
    

    最佳答案

    这是因为subResult1对象的类型为Map,并且值在每个循环中都被覆盖,因为键-相同-被覆盖。由于sub1_arr的所有元素都指向同一对象,因此所有这些元素都将向您显示相同的结果。

    相反,您可能想尝试类似的方法(一种获得json输出的更好方法-不要重新发明轮子):

    import com.fasterxml.jackson.databind.JsonNode;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import com.fasterxml.jackson.databind.node.ArrayNode;
    import com.fasterxml.jackson.databind.node.ObjectNode;
    
    ObjectMapper mapper = new ObjectMapper();
    JsonNode rootNode = mapper.createObjectNode();
    ArrayNode arrayNode = mapper.createArrayNode();
    
    while(rs.next()) {
        ObjectNode layer = mapper.createObjectNode();
        layer.put("layer2_id", rs.getString("layer2_id"));
        layer.put("layer2_name", rs.getString("layer2_name"));
        arrayNode.add(layer);
    }
    
    ((ObjectNode) rootNode).set("layer2", arrayNode);
    String jsonString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(rootNode);
    System.out.println(jsonString);
    

    这是我的测试:
    package com.example;
    
    import java.util.Arrays;
    
    import com.fasterxml.jackson.core.JsonProcessingException;
    import com.fasterxml.jackson.databind.JsonNode;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import com.fasterxml.jackson.databind.node.ArrayNode;
    import com.fasterxml.jackson.databind.node.ObjectNode;
    
    public class Main {
    
        public static void main(String[] args) throws JsonProcessingException {
            ObjectMapper mapper = new ObjectMapper();
            JsonNode rootNode = mapper.createObjectNode();
    
            ObjectNode layer1 = mapper.createObjectNode();
            layer1.put("layer2_id", "1");
            layer1.put("layer2_name", "name_1");
    
            ObjectNode layer2 = mapper.createObjectNode();
            layer2.put("layer2_id2", "2");
            layer2.put("layer2_name2", "name_2");
    
            ObjectNode layer3 = mapper.createObjectNode();
            layer3.put("layer2_id2", "3");
            layer3.put("layer2_name2", "name_3");
    
            ArrayNode arrayNode = mapper.createArrayNode();
            arrayNode.addAll(Arrays.asList(layer1, layer2, layer3));
    
            ((ObjectNode) rootNode).set("layer2", arrayNode);
    
            String jsonString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(rootNode);
            System.out.println(jsonString);
        }
    }
    

    输出:
    {“layer2”:[
    {“layer2_id”:“1”,“layer2_name”:“name_1”},
    {“layer2_id”:“2”,“layer2_name”:“name_2”},
    {“layer2_id”:“3”,“layer2_name”:“name_3”}
    ]}

    09-15 21:29