Search This Blog

Monday, November 12, 2018

JsonObject Java Examples

{
   "firstName":"John",
   "lastName":"Smith",
   "age":25,
   "address":{
      "streetAddress":"21 2nd Street",
      "city":"New York",
      "state":"NY",
      "postalCode":"10021"
   },
   "phoneNumber":[
      {
         "type":"home",
         "number":"212 555-1234"
      },
      {
         "type":"fax",
         "number":"646 555-4567"
      }
   ]
}
can be built using :
JsonObject value = Json.createObjectBuilder()
                .add("firstName", "John")
                .add("lastName", "Smith")
                .add("age", 25)
                .add("address", Json.createObjectBuilder()
                        .add("streetAddress", "88 Main Street")
                        .add("city", "New York")
                        .add("state", "NY")
                        .add("postalCode", "10021"))
                .add("phoneNumber", 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();