- 浏览: 536956 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (174)
- Groovy (28)
- Grails (14)
- DHTML (5)
- JS (4)
- jQuery (16)
- JAVA (16)
- Eclipse (4)
- Hibernate (2)
- Resin (2)
- Oracle (25)
- Maven (4)
- Struts2 (1)
- WebWork2 (3)
- Tomcat (2)
- Windows7 (4)
- Griffon (2)
- PowerDesigner (1)
- Pro*C (2)
- JDK (1)
- 乱码 (2)
- WebService (3)
- .NET (1)
- 性能 (2)
- 安装 (1)
- 命令行 (1)
- function (1)
- type (1)
- Mysql (1)
- 同步 (1)
- Synchronized (1)
- json (3)
- Office (1)
- Visio (1)
- 自定义形状 (1)
- jsong (0)
- gson (1)
- fastjson (1)
- EM (1)
- DB2 (6)
- Sequence (1)
- RHEL5.x (1)
- WAS6.1 ND (1)
- SQL (1)
- -964 (1)
- Linux (4)
- Date (1)
- Format (1)
- Add (1)
- SHELL (1)
- CSS (2)
- Bootstrap (1)
- nginx (1)
- Openresty (2)
- JWPlayer (1)
- showdoc (1)
- 常用网址 (1)
- lua (2)
- SpringBoot (1)
- Office pdf linux (1)
最新评论
-
纵观全局:
Great
阿里巴巴开源JSON解析组件FastJson简单使用笔记 -
guxuede:
...
Groovy编程技巧 -
a1439226817:
groovy用的多吗?我还没发现有哪个java项目在用这个?
Groovy同步 -
晴子9034:
在网上搜到的几乎全是说满了就扩充。但是我有个疑问,满了就扩充, ...
解决DB2 SQLCODE=-964日志文件满的问题 -
在世界的中心呼喚愛:
好东西啊,就用这个包。
阿里巴巴开源JSON解析组件FastJson简单使用笔记
http://memo.feedlr.com/?p=6
Mini guide to rendering JSON with Grails
Grails has built-in support for a JSON building DSL, which, together with the render controller dynamic method, makes rendering JSON responses an enjoyable job. But there seems to be a lack of consolidated information in the official documentation to clarify all the bits about getting anything you want in JSON. So here’s my little mini guide to share with you.
Ground Rules
As the JSON spec reads,
JSON is built on two structures:
A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.
An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.
So there are only two rules for rendering JSON with Grails.
1. To render a collection of name/value pairs (an object), use the method call syntax.
e.g.
render(contentType:'text/json'){
pair(name:'value')
}
Will be rendered as
{"pair":{"name":"value"}}
2. To render an array of values, use a closure
e.g.
render(contentType:'text/json'){
collection{
pair(name:'value')
pair(name:'value1')
}
}
Will be rendered as
{collection:[{"name":"value"},{"name":"value1"}]}
Anything inside the collection closure becomes the values of the array. And because of this, the method name “pair” has no place to appear in the rendered array.
Putting It to Work
Here’s an example putting the two rules together.
render(contentType:'text/json'){
studio(name:'Pixar',website:'pixar.com')
films{
film(title:'Toy Story',year:'1995')
film(title:'Monsters, Inc.',year:'2001')
film(title:'Finding Nemo',year:'2003')
}
}
The result JSON will be
{"studio":{"name":"Pixar","website":"pixar.com"},
"films":[
{"title":"Toy Story","year":"1995"},
{"title":"Monsters, Inc.","year":"2001"},
{"title":"Finding Nemo","year":"2003"}
]}
When DSL is Not Enough
But there’s a gotcha about the current JSON Builder DSL to keep in mind. As of 1.0 RC3, You can have objects inside an array, but not an array inside an object (except the JSON object itself).
For example, I want a JSON structure like this.
{"object":{"collection":[{"name":"value1"},{"name":"value2"}]}}
I may write something like this,
render(contentType:'text/json'){
object(collection{
item(name:'value1')
item(name:'value2')
})
}
Although it’s syntactically correct, you will get a runtime exception of
java.lang.IllegalArgumentException: JSON Builder: not implemented
at grails.util.JSonBuilder.createNode(JSonBuilder.java:121)
...
Oh, maybe I made a mistake, I think. The closure is not a key/value pair there. And I change the code like this
render(contentType:'text/json'){
object(collection:{
item(name:'value1')
item(name:'value2')
})
}
This looks better. Does it work? Nope. It’s not what you’d expect.
{"object":{"collection":"DummyController$_closure15_closure27_closure28@4af40c"}}
This can’t be right. Then how about
render(contentType:'text/json'){
object(collection:collection{
item(name:'value1')
item(name:'value2')
})
}
Oddly, this will be rendered as
{"collection":[{"name":"value1"},{"name":"value2"}],"object":{"collection":1}}
The Almighty JSON converter
So is there any workaround, when JSON Builder DSL fails you? Yes! You can always turn to the JSON converter. Construct a temporary Map to contain the data, and use the converter to render the map as a whole.
To correctly render the JSON data in the previous section, you can use the JSON converter this way. In your controller, use the following code.
import grails.converters.*
...
def jsonMethod = {
...
def json = [object:[collection:[[name:'value1'],[name:'value2']]]]
render json as JSON
}
The result JSON will be exactly what I want
{"object":{"collection":[{"name":"value1"},{"name":"value2"}]}}
You can render any object using the JSON converter. And the results are pretty predictable.
Performance-wise, I’m not sure if there’s any difference or anyone has done any benchmark yet. So I’d say that it’s really your call which way to make your JSON. The converter doesn’t look as sexy as the DSL, but it sure works great. I may just choose either one depending on my mood:)
Tags: dsl, Grails, javascript, json
This entry was posted on Sunday, January 13th, 2008 at 4:11 pm and is filed under Grails. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
Login
Add New Comment
Post as …
Showing 9 comments
Sort by Subscribe by email Subscribe by RSS
Suejo75 1 week ago
How can i read a Json file generated by grails? When i do this my application ask me if i want to open it or down load so anybody can help me?
Like Reply
Merrick BeforeBuffaloDogFood 1 month ago
It is good, I think it is helpful.
Like Reply
Horses For Sale 1 month ago
The "render as JSON" is an excellent workaround and allows for nice refactoring of business logic into services so controller methods simply invoke service calls and render the resulting map.
Like Reply
verunchik 5 months ago
Hallo, can you help me to make this json text:
I need: {"Result":[{"assets":["aaa", "bbb"]},...
but I have: {"Result":[{"assets":"[aaa, bbb]"},...
My code is:
render(contentType:"text/json") {
Result {pair(assets: links)
}}
links is array ["aaa", "bbb"]
Thanks!
Like Reply
Guest 8 months ago
Hi, nice to meet you, my site provide DSL service, i hope you can get some information interesting in my site
Like Reply
Noels 9 months ago
Thanks for the excellent article. That has helped me a great deal. Do you happen to know how I can get an anonymous collection like this: [{"name":"value"},{"name":"value1"}] ?
Like Reply
modestymaster 6 months ago in reply to Noels
Using the builder example above, you may want to consider something like this:
def fakeJSON = []
listOfStuff.each {
fakeJSON.add(["name":it.text()])
}
render fakeJSON as JSON
Mini guide to rendering JSON with Grails
Grails has built-in support for a JSON building DSL, which, together with the render controller dynamic method, makes rendering JSON responses an enjoyable job. But there seems to be a lack of consolidated information in the official documentation to clarify all the bits about getting anything you want in JSON. So here’s my little mini guide to share with you.
Ground Rules
As the JSON spec reads,
JSON is built on two structures:
A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.
An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.
So there are only two rules for rendering JSON with Grails.
1. To render a collection of name/value pairs (an object), use the method call syntax.
e.g.
render(contentType:'text/json'){
pair(name:'value')
}
Will be rendered as
{"pair":{"name":"value"}}
2. To render an array of values, use a closure
e.g.
render(contentType:'text/json'){
collection{
pair(name:'value')
pair(name:'value1')
}
}
Will be rendered as
{collection:[{"name":"value"},{"name":"value1"}]}
Anything inside the collection closure becomes the values of the array. And because of this, the method name “pair” has no place to appear in the rendered array.
Putting It to Work
Here’s an example putting the two rules together.
render(contentType:'text/json'){
studio(name:'Pixar',website:'pixar.com')
films{
film(title:'Toy Story',year:'1995')
film(title:'Monsters, Inc.',year:'2001')
film(title:'Finding Nemo',year:'2003')
}
}
The result JSON will be
{"studio":{"name":"Pixar","website":"pixar.com"},
"films":[
{"title":"Toy Story","year":"1995"},
{"title":"Monsters, Inc.","year":"2001"},
{"title":"Finding Nemo","year":"2003"}
]}
When DSL is Not Enough
But there’s a gotcha about the current JSON Builder DSL to keep in mind. As of 1.0 RC3, You can have objects inside an array, but not an array inside an object (except the JSON object itself).
For example, I want a JSON structure like this.
{"object":{"collection":[{"name":"value1"},{"name":"value2"}]}}
I may write something like this,
render(contentType:'text/json'){
object(collection{
item(name:'value1')
item(name:'value2')
})
}
Although it’s syntactically correct, you will get a runtime exception of
java.lang.IllegalArgumentException: JSON Builder: not implemented
at grails.util.JSonBuilder.createNode(JSonBuilder.java:121)
...
Oh, maybe I made a mistake, I think. The closure is not a key/value pair there. And I change the code like this
render(contentType:'text/json'){
object(collection:{
item(name:'value1')
item(name:'value2')
})
}
This looks better. Does it work? Nope. It’s not what you’d expect.
{"object":{"collection":"DummyController$_closure15_closure27_closure28@4af40c"}}
This can’t be right. Then how about
render(contentType:'text/json'){
object(collection:collection{
item(name:'value1')
item(name:'value2')
})
}
Oddly, this will be rendered as
{"collection":[{"name":"value1"},{"name":"value2"}],"object":{"collection":1}}
The Almighty JSON converter
So is there any workaround, when JSON Builder DSL fails you? Yes! You can always turn to the JSON converter. Construct a temporary Map to contain the data, and use the converter to render the map as a whole.
To correctly render the JSON data in the previous section, you can use the JSON converter this way. In your controller, use the following code.
import grails.converters.*
...
def jsonMethod = {
...
def json = [object:[collection:[[name:'value1'],[name:'value2']]]]
render json as JSON
}
The result JSON will be exactly what I want
{"object":{"collection":[{"name":"value1"},{"name":"value2"}]}}
You can render any object using the JSON converter. And the results are pretty predictable.
Performance-wise, I’m not sure if there’s any difference or anyone has done any benchmark yet. So I’d say that it’s really your call which way to make your JSON. The converter doesn’t look as sexy as the DSL, but it sure works great. I may just choose either one depending on my mood:)
Tags: dsl, Grails, javascript, json
This entry was posted on Sunday, January 13th, 2008 at 4:11 pm and is filed under Grails. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
Login
Add New Comment
Post as …
Showing 9 comments
Sort by Subscribe by email Subscribe by RSS
Suejo75 1 week ago
How can i read a Json file generated by grails? When i do this my application ask me if i want to open it or down load so anybody can help me?
Like Reply
Merrick BeforeBuffaloDogFood 1 month ago
It is good, I think it is helpful.
Like Reply
Horses For Sale 1 month ago
The "render as JSON" is an excellent workaround and allows for nice refactoring of business logic into services so controller methods simply invoke service calls and render the resulting map.
Like Reply
verunchik 5 months ago
Hallo, can you help me to make this json text:
I need: {"Result":[{"assets":["aaa", "bbb"]},...
but I have: {"Result":[{"assets":"[aaa, bbb]"},...
My code is:
render(contentType:"text/json") {
Result {pair(assets: links)
}}
links is array ["aaa", "bbb"]
Thanks!
Like Reply
Guest 8 months ago
Hi, nice to meet you, my site provide DSL service, i hope you can get some information interesting in my site
Like Reply
Noels 9 months ago
Thanks for the excellent article. That has helped me a great deal. Do you happen to know how I can get an anonymous collection like this: [{"name":"value"},{"name":"value1"}] ?
Like Reply
modestymaster 6 months ago in reply to Noels
Using the builder example above, you may want to consider something like this:
def fakeJSON = []
listOfStuff.each {
fakeJSON.add(["name":it.text()])
}
render fakeJSON as JSON
发表评论
-
Grails编写WebService客户端
2012-05-28 13:39 2850环境:jdk1.6.0.31 + Grails2.0.4 1. ... -
解决grails run-app控制台输出乱码问题
2012-04-17 13:14 1766在运行项目时加上 -Dfile.encoding参数,值随OS ... -
(转)使用grag对Grails进行数据库逆向工程
2011-08-04 10:07 1592http://blog.csdn.net/wudith/art ... -
(转)提高Grails应用的查询性能
2011-04-22 17:32 1129转载地址:http://www.groovyq.net/con ... -
(转) 使用Grails进行数据库逆向工程
2010-12-01 14:53 1948原贴地址:http://groovyq.n ... -
(转)让HelpBalloons飘在你的GSP上空
2010-06-09 16:14 1120原贴地址:http://www.groovyq.net/nod ... -
(转)Grails中的命名查询
2010-06-09 16:11 1265原贴地址:http://www.groovyq.net/con ... -
使用createAlias进行“以子对象的属性为查询条件的”查询
2010-06-01 10:52 34def enReservedVenuses = { ... -
利用Hibernate的HibernateCriteriaBuilder建立可分页并带有查询条件的查询
2010-02-26 14:53 76def adminList = { par ... -
Grails应用技巧
2010-02-26 10:58 32331. 领域模型属性复制 ... -
在Grails Console中调用GetBean
2010-01-25 09:57 1200http://www.groovyland.net/?q=no ... -
在Grails console中执行SQL语句
2010-01-22 16:50 28961. import groovy.sql.Sql ... -
Grails分页查询总结
2010-01-06 16:26 407看代码: def list = { param ... -
关于Grails输出JSON的总结
2010-01-06 11:43 19611. def oList = [total:0, ro ... -
Grails+Xfire 搭建Web Service环境
2009-12-24 15:13 1878Grails 加上Xfire plugin 搭建web S ... -
Grails问题汇总
2009-10-28 13:05 63转自:http://hi.baidu.com/ssyuan/b ... -
Grails中直接使用SQL返回List
2009-09-23 10:13 210import groovy.sql.Sql class ... -
Grails按样本查询
2009-07-02 15:35 61主要用到find方法: 方法说明: 指定查询条件(如果没有找 ... -
案例研究:利用Grails搭建Feedlr.com网站
2009-06-29 11:16 1936原贴地址:http://www.infoq ...
相关推荐
Grails 1.3.7英文版官方参考手册,学习Grails的权威指南
《Grails用户手册》 Grails,作为一个基于Groovy语言的开源Web应用框架,深受开发者喜爱,它简化了Java开发的复杂性,提供了强大的MVC(Model-View-Controller)架构,以及丰富的插件系统。这份用户手册将帮助你...
《Grails中文参考手册》是针对Groovy编程语言构建的Web应用框架——Grails的一份详尽学习资料。Grails以其高效、灵活和强大的特性,成为开发人员在Java平台上构建Web应用的热门选择。这份手册旨在帮助初学者快速上手...
Grails超全中文操作手册,无论是大神还是初学者都很适用的操作文档。很全很详细,希望能帮到学习grails框架的你。
"Grails 中文参考手册" 是一套详细的 Grails 学习资料,涵盖了框架的所有核心组件、最佳实践和使用方法,帮助开发者快速掌握 Grails 开发技能。 总之,Grails 是一个强大而灵活的 Web 开发框架,结合 Groovy 的优点...
**Grails 2.4.4 用户手册** **一、Grails 框架概述** Grails 是一个基于 Groovy 语言的开源全栈式Web应用框架,它旨在提高开发效率,提供简洁、灵活的代码结构,以及强大的自动化工具。Grails 2.4.4 是该框架的一个...
### 精通Grails之用JSON和Ajax实现异步Grails #### 一、引言 随着Web 2.0技术的发展,JSON (JavaScript Object Notation) 和 Ajax (Asynchronous JavaScript + XML) 成为现代Web应用开发的重要组成部分。本文旨在...
### Grails 1.0:敏捷、工业级的快速Web应用开发框架 #### 引言 Grails 1.0框架旨在提供一个敏捷且强大的工具集,用于加速Web应用程序的开发过程。它融合了Groovy语言的灵活性与Spring框架的强大功能,以及...
4. **Grails插件**:Grails生态系统中的插件极大地扩展了框架的功能,如Spring Security用于安全控制,GSP(Grails Server Pages)用于视图渲染,以及CouchDB或MongoDB插件实现NoSQL数据库的支持。 5. **...
《Grails 中文参考手册》是一本全面介绍Grails框架的指南,旨在帮助开发者快速上手并深入理解Grails的各个核心概念和技术。Grails是一个基于Groovy语言的开源Web应用框架,它提供了高效的开发环境和强大的功能,使得...
通过阅读《Grails+groovy 完整参考手册.docx》,你可以更深入地了解这两个技术,包括它们的原理、最佳实践以及实际应用中的案例。无论你是初学者还是有经验的开发者,这份手册都将是你学习和提升技能的宝贵资源。
Grails 最新的 v1.1版的中文文档,chm格式,Grails是一套快速开发Web应用的开源框架,基于Groovy编程语言,并构建于Spring、Hibernate和其它标准Java框架之上,能为大家带来超高效率的一站式框架。
**Grails 框架详解** Grails 是一个基于 Groovy 语言的开源Web应用程序框架,它构建在Java平台之...通过阅读《Grails 中文手册》,开发者可以深入理解框架的工作原理,掌握各种开发技巧,从而提高开发效率和代码质量。
在IT行业中,Grails是一个基于Groovy语言的开源Web应用框架,它简化了Java平台上的开发流程。在Grails项目中,版本控制是至关重要的,而Subversion(SVN)是一种常用的版本控制系统,用于管理软件项目的源代码。为了...
Grails开发中文手册,让你学的更轻松!~
**Grails 框架详解** Grails 是一个基于 Groovy 语言的开源Web应用程序框架,它构建在Java平台之上,旨在简化开发过程并提高生产力。Grails 的设计深受Ruby on Rails的影响,提供了MVC(模型-视图-控制器)架构模式...
这通常涉及到将UI渲染的工作卸载到客户端机器上。 - **利用Flex的Rich UI能力**:Flex通过其MXML语言定义用户界面,并使用ActionScript作为编程语言。这样可以轻松创建具有丰富表现力的用户界面。 - **示例分析**:...
《Grails开发手册》是对Grails这一基于Groovy语言的Web应用框架的详细介绍。Grails遵循“约定优于配置”的原则,简化了开发流程,整合了Spring MVC和Hibernate等流行技术,使得开发者无需手动编写数据访问层即可实现...