How do we extract Json in Play 2.8 ? We are upgrading to Play 2.8 framework

Viewed 37

How do we extract Json in play 2.8? I am not finding many examples in Java.

The libraries com.google.gson is not supported any more . JsonArray , JsonElement and JsonObject are not supported any more. I have a method which extracts few values from Json String .

public static synchronized JsonArray getJsonPAIFromString(String jsonString) {
    JsonArray jsonPAIArray = new JsonArray();

    if (jsonString!=null && !jsonString.isEmpty()) {
        JsonElement jelement = new JsonParser().parse(jsonString);
        JsonObject jobject = jelement.getAsJsonObject();
        jobject = jobject.getAsJsonObject("result");
        JsonArray jarray = jobject.getAsJsonArray("policyList");
        for (int i = 0; i < jarray.size(); i++) {
            jobject = jarray.get(i).getAsJsonObject();
            JsonObject pai = jobject.get("pai").getAsJsonObject();
            pai.add("policyHolderId", jobject.get("policyHolderId"));
            pai.add("policyId", jobject.get("id"));
            pai.add("policyNumber", jobject.get("number"));
            jsonPAIArray.add(pai);
            Logger.debug("Response : \n" + pai.toString());
        }
    }
    return jsonPAIArray;
}
1 Answers

Play framework uses Jakson library for JSON transformation. To transform a String into a JSON you can use this:

// parse the JSON as a JsonNode
JsonNode json = Json.parse("{\"firstName\":\"Foo\", \"lastName\":\"Bar\");
Related