Java Reference
In-Depth Information
and JSON objects respectively from scratch;
JsonArray
to access the ordered val-
ues of a JSON array as a list and
JsonObject
to access the values of a JSON
object as a Map and
JsonBuilderFactory
to create
JsonObjectBuilder
or
JsonArrayBuilder
instances;
JsonReader
to read JSON from an input source
and
JsonReaderFactory
to create
JsonReader
instances;
JsonWriter
to write
JSON to an output source, and
JsonWriterFactory
to create
JsonWriter
in-
stances. The following code demonstrates how to create an object model from
scratch and access data within it.
The following code is an example of JSON processing using the object model API:
JsonObject objModel = Json.createObjectBuilder()
.add("student",Json.createObjectBuilder()
.add("id", "854963")
.add("name", "Louis Poyer")
.add("weight", 78.6)
.add("gender","M")
.add("contact",Json.createArrayBuilder()
.add(Json.createObjectBuilder()
.add("address","Rue 632"))
.add(Json.createObjectBuilder()
.add("phone","985-761-0")))
).build();
JsonObject student =
objModel.getJsonObject("student");
String name = student.getString("name");
JsonArray contact =
student.getJsonArray("contact");
String address =
contact.getJsonObject(0).getString("address");
String phone =
contact.getJsonObject(1).getString("phone"));