如何在Grails中将特定日期格式返回为JSON? [英] How to return specific date format as JSON in Grails?

查看:210
本文介绍了如何在Grails中将特定日期格式返回为JSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  render Book.list()as JSON 

渲染结果是

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

您可以通过在Config.groovy中进行设置来控制输出日期。

  grails.converters.json.date ='javascript'//默认或Javascript 

然后结果会是原生javascript日期

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

如果我想获得如下特定的日期格式:

 releaseDate:06-04-2007



<我h ave使用'collect',这需要大量的输入:

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

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

解决方案

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



为了达到你想要的输出,你可以在BootStrap.groovy中注册这样一个ObjectMarshaller:

  import grails.converters.JSON; 

class BootStrap {

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

还有其他几种方法可以自定义转换器和I尽我所能赶上文件尽快。


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?

解决方案

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.

这篇关于如何在Grails中将特定日期格式返回为JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆