Play! Framework -- Parsing JSON

tl;dr I suggest using ObjectMapper found in the com.fasterxml.jackson.databind.ObjectMapper ObjectMapper mapper = new ObjectMapper(); try { JsonNode root = request().body().asJson(); JsonNode jsonPhoneNumbers = root.get("phoneNumbers"); for(JsonNode phoneNumberString : jsonPhoneNumbers) { String phoneNumber = phoneNumberString.asText(); } catch (NullPointerException npe) { // No valid phone numbers were provided } Using ObjectMapper For parsing arrays in JSON requests, I suggest using the ObjectMapper. Assuming you have a request that looks like: { "phoneNumbers": [ "555-555-5555", "555-555-5556", "555-555-5557" ] } We can easily access the phone number values by using ObjectMapper class....

April 6, 2014

Play! Framework -- Returning JSON Responses

I’ve recently started a project at working using the Play Framework and while its a great framework, I was having a lot of trouble with some of the simplest tasks. I wouldn’t blame Play for my problems, returning to Java after a long hiatus, being spoiled by dynamicly typed languages, and lack of documentation really made such tasks like returning a JSON response difficult. I figured that I may not be the only in this position, judging by the questions in the IRC channel and lack of responses, I figured it may be a good idea to jot down some of my notes, not only for myself, my coworkers, but for all my fellow Play framework noobs....

April 6, 2014