This is an example of constructing Json through the object constructing.

Example : Creating Hotels


JsonObject hotel = new JsonObject();
hotel.addProperty("hotelName", "Hilton");
hotel.addProperty("hotelCode", "HIL");

JsonArray rooms = new JsonArray();

JsonObject room = new JsonObject();
room.addProperty("roomid", 1);
room.addProperty("roomCode", "STD");
room.addProperty("roomName", "Standard");
rooms.add(room);

room = new JsonObject();
room.addProperty("roomid", 2);
room.addProperty("roomCode", "DLX");
room.addProperty("roomName", "Delux");

JsonArray mealplans = new JsonArray();

JsonObject mealplan = new JsonObject();
mealplan.addProperty("mealPlanCode", "BB");
mealplan.addProperty("mealPlanName", "Bed & Breakfast");
mealplans.add(mealplan);

mealplan = new JsonObject();
mealplan.addProperty("mealPlanCode", "FP");
mealplan.addProperty("mealPlanName", "Family Plan");
mealplans.add(mealplan);

mealplan = new JsonObject();
mealplan.addProperty("mealPlanCode", "AI");
mealplan.addProperty("mealPlanName", "All-Inclusive");
mealplans.add(mealplan);

room.add("mealPlans", mealplans);

rooms.add(room);

hotel.add("rooms", rooms);

Result :

Screenshot from 2014-02-23 10:22:24

Reference :

Advertisements