Sparkles

that were shone when I got tempered!

Creating a json string using gson – via Object creation

leave a comment »

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

Written by Namal Fernando

February 23, 2014 at 10:03 am

Posted in gson API, Json

Tagged with ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Ruth's Reflections

Contemplations from quakey quirky Christchurch

TED Blog

The TED Blog shares interesting news about TED, TED Talks video, the TED Prize and more.

Ziplok

Learn and discover simple things

Meihta Dwiguna Saputra's Knowledge Base

~In learning you will teach and in teaching you will (re)learn~

The Java Blog

Thoughts, tips and tricks about the Java programming language

Sparkles

that were shone when I got tempered!

%d bloggers like this: