August 17, 2014

How to Produce JSON Properly from Spring

Not that the default spring JSON is that bad. It looks like this:

[{ 2.62779739789553,1556.68506945,'El Pollo Loco'},{4.087178144481979,1632.109670148,'Paper or Plastik Cafe'}
I don't like this and want something more like:
[{'azimuth': 1.3775424158235956,  'distance': 625.924396521,  'name': 'Starbucks'}, {'azimuth': 1.628478725514169,  'distance': 646.038250929,  'name': 'Asian Cuisine'}]
And I figured it out:

for (results.next(); results.isAfterLast() == false; results.next()) {
        Spot spot = new Spot();
 spot.setAzimuth(results.getDouble("bearing"));
 spot.setDistance(results.getDouble("distance"));
 spot.setName(results.getString("name"));
 LOGGER.debug(spot.toString());
 spots.add(spot);
}
Yes, by making it at list of a bean I wrote, instead of retrieving it directly into a collection of them, it seems I can force a hash as output from spring.

No comments:

Post a Comment