0

I've got multiple console logs. The "AB" log in the ArrayBuilder itself gives me the output below which is the correct output. But the log in the input for the ArrayBuilder gives me a blank [ ]. Why is the JsonArrayBuilder not returning the correct output?

"AB[{"ingredientnaam":"Banaan","calorieen":89,"vet":0.9,"verzadigd_vet":0.3,"eiwit":1.2,"koolhydraten":20.4,"vezels":1.9,"zout":0.0}]"

Input for the ArrayBuilder

JsonArray ingredientArray = buildJsonIngredientArray(service.getToday(gebruikersnaam, datum));
System.out.println(ingredientArray.toString());
return ingredientArray.toString();

My JsonArrayBuilder

private JsonArray buildJsonIngredientArray(List<Dagboek> list) {
    JsonArrayBuilder jsonArrayBuilder = Json.createArrayBuilder();

    for (Dagboek d : list) {
        Ingredient c = d.getIngredient();
        JsonObjectBuilder job = Json.createObjectBuilder();
        job.add("ingredientnaam", c.getIngredientnaam());
        job.add("calorieen", c.getCalorieen());
        job.add("vet", c.getVet());
        job.add("verzadigd_vet", c.getVerzadigd_vet());
        job.add("eiwit", c.getEiwit());
        job.add("koolhydraten", c.getKoolhydraten());
        job.add("vezels", c.getVezels());
        job.add("zout", c.getZout());

        jsonArrayBuilder.add(job);
    }
    System.out.println("AB" + jsonArrayBuilder.build());
    return jsonArrayBuilder.build();
}
3
  • 2
    This seems unlikely, but it might be clearing the internal array after you call build. Try storing the return value for build used here: System.out.println("AB" + jsonArrayBuilder.build());, and returning it. (full disclosure, no idea if this will work) Commented Jun 13, 2017 at 15:46
  • @NeilLocketz What the hell, I don't understand why but it fixed it haha. Thanks a bunch! Commented Jun 13, 2017 at 15:56
  • No problem, I'll answer the question too. Commented Jun 13, 2017 at 15:57

1 Answer 1

1

It might be clearing the internal array after you call build. I couldn't find anything about this in the documentation though.

Try storing the return value for build used here:

System.out.println("AB" + jsonArrayBuilder.build());

and returning it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.