grailsgson-JSON视图
我想生成类似以下内容的JSON输出:grailsgson-JSON视图,json,grails,view,gson,Json,Grails,View,Gson,我想生成类似以下内容的JSON输出: var json = { id: "1", name: "AAA", children: [{ id: "2", name: "BBB", data: { relation: "<b>Connections:</b><ul><li> AAA <div>
var json = {
id: "1",
name: "AAA",
children: [{
id: "2",
name: "BBB",
data: {
relation: "<b>Connections:</b><ul><li> AAA <div>(relation: direct)</div></li><li> CCC <div>(relation: direct)</div></li></ul>"
},
children: [{
id: "3",
name: "CCC",
data: {
relation: "<b>Connections:</b><ul><li> BBB <div>(relation: direct)</div></li></ul>"
},
children: []
}]
}, ....
_object.gson
问题 我无法制作:
data: {
relation: "<b>Connections:</b><ul><li> BBB <div>(relation: direct)</div></li></ul>"
},
数据:{
关系:“连接:- BBB(关系:直接)
”
},
问题:
1) 我已经阅读了官方文档,但我找不到如何处理瞬态字段
2) 混合json和xml/html的最佳方法是什么。
3) 如何将json代码传递给另一个视图变量
提前感谢您您可以通过以下网址查看有关gson视图的官方文档: 域类
class Object{
String name
String relation
static hasMany = [children: Object]
public String getRelation() {
模板1对象.gson
import json.Object
model {
Object object
}
json tmpl.object(object)
import json.Object
model {
Object object
}
json {
id object.id
data(relation: object.relation)
name object.name
children g.render(object.children,[ excludes:['exclude_fields']])
//children g.render(object.children,[resolveTemplate: false]) // one to many relations - avoid circular error
//object2 object.book.name // one to one relations
}
模板2\u object.gson
import json.Object
model {
Object object
}
json tmpl.object(object)
import json.Object
model {
Object object
}
json {
id object.id
data(relation: object.relation)
name object.name
children g.render(object.children,[ excludes:['exclude_fields']])
//children g.render(object.children,[resolveTemplate: false]) // one to many relations - avoid circular error
//object2 object.book.name // one to one relations
}
ObjectController
import grails.plugin.json.view.JsonViewTemplateEngine
import org.springframework.beans.factory.annotation.Autowired
@Autowired
JsonViewTemplateEngine templateEngine
def test() {
def t = templateEngine.resolveTemplate('/object/object')
def writable = t.make(object: Object.get(params.id))
def sw = new StringWriter()
writable.writeTo( sw )
return [json:sw]
}
问题:
1) 我已经阅读了官方文档,但找不到如何创建临时字段-您可以使用命名参数,这些参数是对象或getter的有效值
class Object{
String name
String relation
static hasMany = [children: Object]
public String getRelation() {
2) 混合json和xml/html的最佳方法是什么**我想你可以查一下:**
3) 如何将json代码传递给另一个视图变量-**在objectController上检查上面的代码**