Home > database >  JsonArray is not adding all JsonObjects
JsonArray is not adding all JsonObjects

Time:06-28

When I add a list of test JsonObjects, only the last JsonObject is add to the JsonArray. I do not understand why because I am following documentation from oracle. https://docs.oracle.com/javaee/7/api/javax/json/JsonArray.html

Methods from class called TestRun

public JsonObject convertToJSONObject() {
        return Json.createObjectBuilder()
                .add("name", name)
                .add("value", value)
                .add("timestamp", convertZonedDateTime())
                .build();
    }

    public JsonArray convertToJSONArray(JsonObject object) {
        return Json.createArrayBuilder()
                .add(object)
                .build();
    }

Main (Note: I am using faker to generate random info)

TestRun testRun;
        Faker faker = new Faker(); //Faker to generate random level_name
        JsonArray jsonArray = null;


        for (int i = 0; i < 2; i  ) {
            testRun = new TestRun(faker.name().firstName(), faker.number().numberBetween(1, 200));
            JsonObject object = testRun.convertToJSONObject();
            jsonArray = testRun.convertToJSONArray(object);
        }
        System.out.println(jsonArray);

CodePudding user response:

You need to create an JsonArrayBuilder inside for loop instead of creating JsonArray at each step. Then build the builder outside of for loop like:

JsonArrayBuilder arrayBuilder = null;
for (int i = 0; i < 2; i  ) {
      testRun = new TestRun(faker.name().firstName(), faker.number().numberBetween(1, 200));
      JsonObject jsonObject = testRun.convertToJSONObject();
      if (arrayBuilder == null) {
         arrayBuilder = Json.createArrayBuilder().add(jsonObject);
      } else {
         arrayBuilder = arrayBuilder.add(jsonObject);
      }

}

jsonArray = arrayBuilder.build();

CodePudding user response:

TestRun.java

import javax.json.Json;
import javax.json.JsonObject;
import java.time.ZonedDateTime;

public class TestRun {
    private String name;
    private Integer value;
    private ZonedDateTime timestamp;

    public TestRun(String name, Integer value, ZonedDateTime timestamp) {
        this.name = name;
        this.value = value;
        this.timestamp = timestamp;
    }

    public JsonObject convertToJSONObject() {
        return Json.createObjectBuilder()
                .add("name", name)
                .add("value", value)
                .add("timestamp", convertZonedDateTime())
                .build();
    }

    private String convertZonedDateTime() {
        return timestamp.toString(); // replace with yours code
    }
}

Class with main method:

import com.github.javafaker.Faker;
import javax.json.Json;
import javax.json.JsonArrayBuilder;
import javax.json.JsonObject;
import java.time.ZonedDateTime;

public class FakerExample {

    public static void main(String[] args) {
        Faker faker = new Faker();
        JsonArrayBuilder jsonArrayBuilder = Json.createArrayBuilder();

        for (int i = 0; i < 2; i  ) {
            var testRun = new TestRun(faker.name().firstName(), faker.number().numberBetween(1, 200), ZonedDateTime.now());
            JsonObject object = testRun.convertToJSONObject();
            jsonArrayBuilder.add(object);
        }

        System.out.println(jsonArrayBuilder.build());
    }
}
  • Related