manojadams
manojadams

Reputation: 2430

Error while converting JsonObject to JsonArray

Getting exception with the following code: org.glassfish.json.JsonStringImpl cannot be cast to javax.json.JsonArray

JsonObjectParser jop = new JsonObjectParser();
JsonObject jo = Json.createObjectBuilder().add("yAxisValue","Yvalue")
            .add("xAxisValue","Xvalue").build();
jo.getJsonArray("xAxisValue");

Note: using javax.json java API

Upvotes: 0

Views: 249

Answers (2)

Deepu--Java
Deepu--Java

Reputation: 3820

For creating JSON Array by javax.json use following method.

JsonArray value = Json.createArrayBuilder()
     .add(Json.createObjectBuilder()
         .add("type", "home")
         .add("number", "212 555-1234"))
     .add(Json.createObjectBuilder()
         .add("type", "fax")
         .add("number", "646 555-4567"))
     .build();

Upvotes: 1

Javy
Javy

Reputation: 964

The error is the following

.add("xAxisValue","Xvalue")

the statement above is not to create a json array, the json string you created is the following

{ 
  "yAxisValue":"Yvalue",
  "xAxisValue":"Xvalue"
}

not

{ 
  "yAxisValue":"Yvalue",
  "xAxisValue":["Xvalue"]
}

you should use the function add("key",Json.createArrayBuilder(...))

hope that helped

Upvotes: 1

Related Questions