Javajson - opensas/Play20Es GitHub Wiki
Esta página todavía no ha sido traducida al castellano. Puedes ayudarnos con la tarea simplemente presionando el botón
Edit Page. Para más información puedes leer esta guía para el traductor. Aquí puedes ver cuánto nos falta para terminar la traducción.
Play includes the Jackson library as a dependency (A good tutorial of Jackson can be found here). What this means in practice is that one can serialize from and to JSON format.
As for rendering JSON, there is a helper method play.json.Render.toJson that can be used to send JSON as a response. For example:
import play.*;
import play.mvc.*;
import static play.libs.Json.toJson;
import java.util.Map;
import java.util.HashMap;
public class MyController extends Controller {
public static Result index() {
Map<String,String> d = new HashMap<String,String>();
d.put("peter","foo");
d.put("yay","value");
return ok(toJson(d));
}
}
The code above can be rewritten for brevity using the Google Guava collections library:
import com.google.common.collect.ImmutableMap;
import play.*;
import play.mvc.*;
import static play.libs.Json.toJson;
public class MyController extends Controller {
public static Result index() {
return ok(toJson(ImmutableMap.of(
"peter", "foo",
"yay", "value")));
}
}