0

I am trying to create an JSON Array using Java EE 7 libraries.

import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonObject;

jsonregistration=(JsonArray) Json.createArrayBuilder()
   .add("APA91bE58Q")
   .add("APA91bGT_sd")
   .build();

Works perfectly fine Assuming i have tons of create the list and it is read from database. When i tried to iterate the code, it doesn't work.

jsonregistration=(JsonArray) Json.createArrayBuilder();
while(rs.next())
{
    jsonregistration.add(rs.getString(1));
}
jsonregistration.build();

Doesn't work.

2
  • 1
    What doesn't work? it doesn't add any thing to array? if so might be your resultset don't have anything in it to add. Commented Apr 8, 2016 at 8:12
  • No my resultset returns a value. just that when jsonregistration.add(rs.getString(1)); it returns an error no suitable method found for add(String) method Collection.add(JsonValue) is not applicable (argument mismatch; String cannot be converted to JsonValue) Commented Apr 8, 2016 at 8:36

1 Answer 1

1

Json.createArrayBuilder() returns an object of type JSONArrayBuilder so you have to write your code like this and it will work

JsonArrayBuilder jsonregistration= Json.createArrayBuilder();
while(rs.next())
{
    jsonregistration.add(rs.getString(1));
}
JsonArray jsonArray =  jsonregistration.build();
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.