如何在 Grails 中以 JSON 形式返回特定日期格式?

发布于 2024-07-16 11:14:57 字数 979 浏览 4 评论 0原文

在 Grails 中,您可以使用 JSON 转换器在控制器中执行此操作:

render Book.list() as JSON

渲染结果为

[
{"id":1,
 "class":"Book",
 "author":"Stephen King",
 "releaseDate":'2007-04-06T00:00:00',
 "title":"The Shining"}
]

You can control the output date by make a setting in Config.groovy

grails.converters.json.date = 'javascript' // default or Javascript

然后结果将是本机 javascript 日期

[
{"id":1,
 "class":"Book",
 "author":"Stephen King",
 "releaseDate":new Date(1194127343161),
 "title":"The Shining"}
]

如果我想获取特定的 日期像这样的日期格式:

"releaseDate":"06-04-2007"

我必须使用“收集”,这需要大量输入:

return Book.list().collect(){
  [
      id:it.id,
      class:it.class,
      author:it.author,
      releaseDate:new java.text.SimpleDateFormat("dd-MM-yyyy").format(it.releaseDate),
      title:it.title
  ]
} as JSON

有没有更简单的方法来做到这一点?

In Grails, you can use the JSON converters to do this in the controller:

render Book.list() as JSON

The render result is

[
{"id":1,
 "class":"Book",
 "author":"Stephen King",
 "releaseDate":'2007-04-06T00:00:00',
 "title":"The Shining"}
]

You can control the output date by make a setting in Config.groovy

grails.converters.json.date = 'javascript' // default or Javascript

Then the result will be a native javascript date

[
{"id":1,
 "class":"Book",
 "author":"Stephen King",
 "releaseDate":new Date(1194127343161),
 "title":"The Shining"}
]

If I want to get a specific date format like this:

"releaseDate":"06-04-2007"

I have to use 'collect', which requires a lot of typing:

return Book.list().collect(){
  [
      id:it.id,
      class:it.class,
      author:it.author,
      releaseDate:new java.text.SimpleDateFormat("dd-MM-yyyy").format(it.releaseDate),
      title:it.title
  ]
} as JSON

Is there a simpler way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

滴情不沾 2024-07-23 11:14:57

有一个简单的解决方案:从 Grails 1.1 开始,转换器已被重写为更加模块化。 不幸的是我没有完成这方面的文档。 现在它允许注册所谓的 ObjectMarshallers(实现 org.codehaus.groovy.grails.web.converters.marshaller.ObjectMarshaller 接口的简单 Pogo/Pojo)。

为了实现您想要的输出,您可以通过这种方式在 BootStrap.groovy 中注册这样一个 ObjectMarshaller:

import grails.converters.JSON;

class BootStrap {

     def init = { servletContext ->
         JSON.registerObjectMarshaller(Date) {
            return it?.format("dd-MM-yyyy")
         }
     }
     def destroy = {
     }
}

还有其他几种方法可以自定义转换器的输出,我会尽力尽快赶上文档。

There is a simple solution: Since Grails 1.1 the Converters have been rewritten to be more modular. Unfortunately I didn't finish the documentation for that. It allows now to register so called ObjectMarshallers (simple Pogo/Pojo's that implement the org.codehaus.groovy.grails.web.converters.marshaller.ObjectMarshaller interface).

To achieve your desired output, you could register such an ObjectMarshaller in BootStrap.groovy that way:

import grails.converters.JSON;

class BootStrap {

     def init = { servletContext ->
         JSON.registerObjectMarshaller(Date) {
            return it?.format("dd-MM-yyyy")
         }
     }
     def destroy = {
     }
}

There are several other ways to customize the output of the Converters and I'll do my best do catch up with the documentation asap.

浊酒尽余欢 2024-07-23 11:14:57

或者您可以在日期级别本身工作。 这可能不完全是您想要的,但它可能会激发一个在整个应用程序中一致工作的解决方案的想法。

def doWithDynamicMethods = {ctx ->

  def customDateToString = {->
        def dateFormat = "dd MMM yyyy"
        def timeFormat = "hh:mm:ss a"

        def timeCheck = new java.text.SimpleDateFormat("hh:mm:ss SSS a")
        def formattedTime = timeCheck.format(delegate)
        def formatString = dateFormat
        if (formattedTime != "12:00:00 000 AM") 
                              formatString = "$formatString $timeFormat"
        def formatter = new java.text.SimpleDateFormat("$formatString")
        formatter.format(delegate)
    }

    Date.metaClass.toString = customDateToString;
    java.sql.Timestamp.metaClass.toString = customDateToString;
}

Or you could work at the Date level itself. This might not be exactly what you want but it could spark an idea for a solution that would work consistently across your whole app.

def doWithDynamicMethods = {ctx ->

  def customDateToString = {->
        def dateFormat = "dd MMM yyyy"
        def timeFormat = "hh:mm:ss a"

        def timeCheck = new java.text.SimpleDateFormat("hh:mm:ss SSS a")
        def formattedTime = timeCheck.format(delegate)
        def formatString = dateFormat
        if (formattedTime != "12:00:00 000 AM") 
                              formatString = "$formatString $timeFormat"
        def formatter = new java.text.SimpleDateFormat("$formatString")
        formatter.format(delegate)
    }

    Date.metaClass.toString = customDateToString;
    java.sql.Timestamp.metaClass.toString = customDateToString;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文