You would probably do this automatically with some library. But I am new with Java and JSON and I need a quick sollution.
What I would like is to write down (echo) JSON out of a JSP file. So far so good, but now I have a list of objects. So I start a fast enumeration.
And now the question: how do I close the JSON array with }]
instead of ,
? Normally I put a nill or null in the and.
Here is my loop:
"rides":[{
<%
List<Ride> rides = (List<Ride>)request.getAttribute("matchingRides");
for (Ride ride : rides) {
%>
"ride":{
"rideId":"<%= String.valueOf(ride.getId()) %>",
"freeText":"<%= freeText %>"
},
<%
}
%>
} ]