gpt4 book ai didi

json - Grails GSON JSON View

转载 作者:行者123 更新时间:2023-12-02 14:29:37 25 4
gpt4 key购买 nike

我想生成与此类似的JSON输出:

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: []
}]
}, ....

这是我到目前为止所做的:
grails create-domain-resource json.Object
class Object{
String name
String relation
static hasMany = [children: Object]

public String getData() {
def writer = new StringWriter()

_object.gson
//json g.render(hero)
json {
//data hero.data
data: {relation hero.relation}
name hero.name
}

问题
我无法产生:
 data: {
relation: "<b>Connections:</b><ul><li> BBB <div>(relation: direct)</div></li></ul>"
},

问题:
1)我已经阅读了官方文档,但无法找到 transient 字段
2)什么是混合json和xml / html的最佳方法。
3)如何将json代码传递给另一个 View 变量

先感谢您

最佳答案

您可以在以下位置查看官方文档以获取gson View :

http://views.grails.org/latest/#_json_view_api

域类

class Object{
String name
String relation
static hasMany = [children: Object]

public String getRelation() {

模板1 object.gson
import json.Object

model {
Object object
}

json tmpl.object(object)

模板2 _object.gson
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)我已经阅读了官方文档,但无法找到过渡字段- ,您可以使用命名参数,这些参数是对象或 setter/getter 的有效值

2)什么是混合json和xml / html的最佳方法。 -**我想您可以检查: http://docs.groovy-lang.org/latest/html/gapi/groovy/json/StreamingJsonBuilder.html **

3)如何将json代码传递给另一个 View 变量-**在objectController处检查以上代码**

关于json - Grails GSON JSON View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44134709/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com