gpt4 book ai didi

java - 循环生成 JSON

转载 作者:行者123 更新时间:2023-12-01 14:31:52 25 4
gpt4 key购买 nike

我正在实现 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);
    }
    }

    Output:
    {"layer2":[
    {"layer2_id" : "1", "layer2_name" : "name_1"},
    {"layer2_id" : "2", "layer2_name" : "name_2"},
    {"layer2_id" : "3", "layer2_name" : "name_3"}
    ]}

    关于java - 循环生成 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62481089/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com