Reputation:
I'm new to Play Framework, and having trouble rendering a JSON object.
public static void LoginFail() {
Object[][] statusArray = {
{"Status", "401"},
{"Message", "Unauthorized"},
{"Detail", "No API Key Supplied"}
};
renderJSON(statusArray);
}
This only displays [[{},{}],[{},{}],[{},{}]]
...what am I doing wrong? I can't find any solid documentation on this. I tried configuring the route for Application.LoginFail(format:'json')
, but this did nothing.
Upvotes: 7
Views: 10683
Reputation: 303
Here is what you can do
import play.libs.Json;
If you are reading JSON from Browser as HTTP Body then
JsonNode json = request().body().asJson();
Program program = Json.fromJson(json, Program.class);
Here Program can be your entity class or data transport object.
If you have to fetch records and send it to browser in JSON then do as below
Program program = ProgramDAO.findById(id);
if(program!=null){
result = ok(Json.toJson(program));
}
Hope this helps
Upvotes: 0
Reputation: 526
the best in this case is used a HashMap:
public static void LoginFail() {
Map<String, String> status = new HashMap<String, String>();
status.put("Status", "401");
status.put("Message", "Unauthorized");
status.put("Detail", "No API Key Supplied");
renderJSON(status);
}
You can also use another strategy, which is to define an object with the definition of what you want to return and render this:
public class Status{
public String status, message, detail;
public Status(String status, String message, String detail){
this.status = status;
this.message = message;
this.detail = detail;
}
}
public static void LoginFail(){
Status status = new Status("401", "Unauthorized", "No API Key Supplied");
renderJSON(status);
}
Upvotes: 0
Reputation: 11540
Do it the simple & reusable way by creating a StatusMessage object
public class StatusMessage {
public String status;
public String message;
public String detail;
public StatusMessage(String status, String message, String detail) [
this.status = status;
this.message = message;
this.detail = detail;
}
}
And then
renderJSON(new StatusMessage("401", "Unauthorized", "No API Key Supplied"));
Upvotes: 14
Reputation: 1810
From the looks of your code it seems that your are trying the create a JSON string by yourself, using an array of type Object. My only guess as to why this doesn't work is that GSON (the JSON library in use by play) doesn't know how to convert that to key-value pairs (although your array is 2-dimensional). So how about changing statusArray
to String and its content to:
{
"Status": "401",
"Message": "Unauthorized",
"Detail": "No API Key Supplied"
}
Put that into renderJSON(statusArray)
and you should be fine.
As an alternative you could create a simple .json
template like the following:
{
"Status": ${status},
"Message": ${message},
"Detail": ${detail}
}
and call it from a controller method via render(status, message, detail)
. status
, message
and detail
being Strings here as well. Example controller method:
public static void loginFail(final String status, final String message, final String detail) {
render(status, message, detail);
}
and your template would be called loginFail.json
(the name of the controller method). That way you can call the controller method in whatever logic you have to verify the login. Once the login fails you specify why that is (via status, message and details) by calling the loginFail
method.
Upvotes: 11