简体   繁体   English

JsonArray 在 Java 的 for 循环中仅采用最后一个 JsonObject

[英]JsonArray taking only last JsonObject inside a for loop in Java

I need to modify an existing old code that uses javax.json.JsonObject and javax.json.JsonArray library to handle JSON.我需要修改使用javax.json.JsonObjectjavax.json.JsonArray库的现有旧代码来处理 JSON。 Since these are interfaces so I cannot create a new object and add the required data inside for loop as I have done in the past with the help of JSON.simple libraries.由于这些是接口,所以我无法创建一个新的 object 并在 for 循环中添加所需的数据,就像我过去在 JSON.simple 库的帮助下所做的那样。 I need to create a JsonArray that contains different JsonObjects but the final output contains only the last JsonObject.我需要创建一个包含不同 JsonObjects 但最终 output 仅包含最后一个 JsonObject 的 JsonArray。 Here is the code:这是代码:

import javax.json.JsonArray;
import javax.json.JsonObject;
class Test {
    public void myMethod() {
        Map<String, String> ep = null;
        JsonArray response = null;
        JsonObject epObj = null;
        JsonObject epDetails = null;
        try {
            ep = getRequiredEp();
            if (ep != null && !ep.isEmpty()) {
                for (String uuid : ep.keySet()) {
                    ERU router = ERU.findByNFuuid(em, uuid);
                    if (router != null) {
                        ERD unit = router.findCurrent(em);
                        epDetails = Json.createObjectBuilder()
                                .add("name", ep.get(uuid))
                                .add("devId", unit.unit.getId())
                                .add("clientId", unit.store.id).build();
                        epObj = Json.createObjectBuilder()
                                .add(uuid, epDetails).build();
                        response = Json.createArrayBuilder()
                                .add(epObj).build();
                    }
                }
                logger.info("FINAL RESPONSE = {}", response);
            }
        } catch (Exception e) {
            logger.info(e.getMessage(), e);
        }
    }
}

My JsonObject contains two objects.我的 JsonObject 包含两个对象。 But when I print Final response it give only the last object.但是当我打印最终响应时,它只给出最后一个 object。 Please suggest how to add both the objects in the JsonArray.请建议如何在 JsonArray 中添加这两个对象。

That's because you are assigning a new JsonArray to the response on each iteration.那是因为您在每次迭代时为响应分配了一个新的 JsonArray。 I think you should use the single array builder inside your loop and build the array outside, like that:我认为您应该在循环内使用单个数组构建器并在外部构建数组,如下所示:

public void myMethod() {
    Map<String, String> ep = null;
    var builder = Json.createArrayBuilder();
    JsonArray response = null;
    JsonObject epObj = null;
    JsonObject epDetails = null;
    try {
        ep = getRequiredEp();
        if (ep != null && !ep.isEmpty()) {
            for (String uuid : ep.keySet()) {
                ERU router = ERU.findByNFuuid(em, uuid);
                if (router != null) {
                    ERD unit = router.findCurrent(em);
                    epDetails = Json.createObjectBuilder()
                            .add("name", ep.get(uuid))
                            .add("devId", unit.unit.getId())
                            .add("clientId", unit.store.id).build();
                    epObj = Json.createObjectBuilder()
                            .add(uuid, epDetails).build();
                    builder.add(epObj);
                }
            }
            response = builder.build();
            logger.info("FINAL RESPONSE = {}", response);
        }
    } catch (Exception e) {
        logger.info(e.getMessage(), e);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM