Reputation: 219
My Main resource file
public class MyResource {
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/getAll")
public List<hotels> getResources() throws IOException {
hotelServices services = new hotelServices();
return services.getAllHotels();
}}
This is how I am assigning values and returning the List
public class hotelServices {
public List<hotels> getAllHotels() throws IOException
{
List<hotels> list = new ArrayList<hotels>();
String ID = "73";
String HoteName = "KKR"
String Location = "Kelambakkam"
String Ratings = "2"
String Landmark = "Opp to R&G"
hotels thisHotel = new hotels(ID,HotelName,Location,Ratings,Landmark);
list.add(thisHotel);
return list;
} }
And below is how my constructor hotels will look like
public hotels(String id, String hotelName,String location, String ratings, String landmark)
{
this.id = id;
this.hotelName = hotelName;
this.location=location;
this.ratings = ratings;
this.landmark = landmark;
}
Am getting a response something like this
[{
"hotelName":" KKR",
"id":" 73",
"landmark":" Opp to R&G",
"location":" Kelambakkam",
"ratings":" 2"
}]
Am trying to generate something like this with Json objects which I am unable to do. can any help me please??
{"hotels":[{
"hotelName":" KKR",
"id":" 73",
"landmark":" Opp to R&G",
"location":" Kelambakkam",
"ratings":" 2"
}]}
Upvotes: 1
Views: 623
Reputation: 32046
What you're expecting is a wrapper to the List<Hotels>
for which you can define a model say your already existing one - HotelServices
as -
class HotelServices {
List<Hotels> hotels; // do all the logic of setting this value as you currently do
}
and modify your resource as :
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/getAll")
public HotelServices getResources() throws IOException {
HotelServices services = new HotelServices(); // assuming you would assign value to this
return services;
}}
Note: Renaming by me to try and follow better naming conventions.
Upvotes: 1