简体   繁体   English

Play Framework renderJSON问题

[英]Play Framework renderJSON Issue

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')配置路由Application.LoginFail(format:'json') ,但这没有做任何事情。

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"));

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. 从您的代码看起来,您似乎正在尝试使用Object类型的数组自己创建一个JSON字符串。 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). 我唯一的猜测是为什么这不起作用是GSON(播放使用的JSON库)不知道如何将其转换为键值对(尽管你的数组是二维的)。 So how about changing statusArray to String and its content to: 那么如何将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) . 并通过render(status, message, detail)从控制器方法调用它。 status , message and detail being Strings here as well. statusmessagedetail也是字符串。 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). 并且您的模板将被称为loginFail.json (控制器方法的名称)。 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方法指定原因(通过状态,消息和详细信息)。

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);
}

Here is what you can do 这是你可以做的

import play.libs.Json;

If you are reading JSON from Browser as HTTP Body then 如果您正在从浏览器中读取JSON作为HTTP Body,那么

 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 如果您必须获取记录并使用JSON将其发送到浏览器,请执行以下操作

  Program program = ProgramDAO.findById(id);
        if(program!=null){
            result = ok(Json.toJson(program));
        }

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM