java Play 框架 renderJSON 问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3722923/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Play Framework renderJSON Issue
提问by seb
I'm new to Play Framework, and having trouble rendering a JSON object.
我是 Play Framework 的新手,无法呈现 JSON 对象。
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.
这只显示[[{},{}],[{},{}],[{},{}]]
......我做错了什么?我找不到任何关于此的可靠文档。我尝试为 配置路由Application.LoginFail(format:'json')
,但这没有任何作用。
回答by Damo
Do it the simple & reusable way by creating a StatusMessage object
通过创建 StatusMessage 对象以简单且可重用的方式进行
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"));
回答by seb
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:
从代码的外观来看,您似乎正在尝试使用 Object 类型的数组自己创建一个 JSON 字符串。关于为什么这不起作用,我唯一的猜测是 GSON(播放使用的 JSON 库)不知道如何将其转换为键值对(尽管您的数组是二维的)。那么如何更改statusArray
为 String 及其内容:
{
"Status": "401",
"Message": "Unauthorized",
"Detail": "No API Key Supplied"
}
Put that into renderJSON(statusArray)
and you should be fine.
把它放进去renderJSON(statusArray)
,你应该没问题。
As an alternative you could create a simple .json
template like the following:
作为替代方案,您可以创建一个简单的.json
模板,如下所示:
{
"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:
并通过控制器方法调用它render(status, message, detail)
。status
,message
并detail
在这里被字串。示例控制器方法:
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.
并且您的模板将被调用loginFail.json
(控制器方法的名称)。这样您就可以在验证登录所需的任何逻辑中调用控制器方法。一旦登录失败,您可以通过调用该loginFail
方法指定原因(通过状态、消息和详细信息)。
回答by Maria Mercedes Wyss Alvarez
the best in this case is used a HashMap:
在这种情况下最好使用 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);
}
回答by sunny
Here is what you can do
这是你可以做的
import play.libs.Json;
If you are reading JSON from Browser as HTTP Body then
如果您从浏览器中读取 JSON 作为 HTTP 正文,那么
JsonNode json = request().body().asJson();
Program program = Json.fromJson(json, Program.class);
Here Program can be your entity class or data transport object.
这里的 Program 可以是您的实体类或数据传输对象。
If you have to fetch records and send it to browser in JSON then do as below
如果您必须获取记录并将其以 JSON 格式发送到浏览器,请执行以下操作
Program program = ProgramDAO.findById(id);
if(program!=null){
result = ok(Json.toJson(program));
}
Hope this helps
希望这可以帮助