简体   繁体   中英

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. 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. I need to create a JsonArray that contains different JsonObjects but the final output contains only the last JsonObject. 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. But when I print Final response it give only the last object. Please suggest how to add both the objects in the JsonArray.

That's because you are assigning a new JsonArray to the response on each iteration. 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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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