我見過一些非常酷的Groovy和Grails JSON構建器的例子。這裏有一個: http://www.objectpartners.com/2012/03/22/grails-protip-dynamically-creating-json-in-grails-2-0-with-jsonbuilder/使用Groovy JSON構建器
現在,我正在使用控制器生成測試列表使用收集,然後呈現列表到一個JSON對象。有誰知道如何把上面的例子,並將其放入控制器?
這裏是我的控制器是什麼樣子:
class TreeMapController {
def list(){
def results = myDomain.list()
def test = [:] //Test List
test=[]
def i = 0 //index key for parent
//Generate list for fancyTree
for (record in results){
test.add([key:i++,folder:true, title: record.name, guid: record.id,
children:record.subResults.collect{[title:it.name]}
])
}
//render response types
withFormat {
html test
json {render test as JSON}
xml {render test as XML}
}
}
}
要調用這個使用JSON要求我提供的鏈接:本地主機/項目/ list.json ,如果我打電話給提供(鏈接到使用JSON的例子建設者)以上我將如何打電話或提出請求。
不知道我明白在最後一段中的問題? –
我想說的是,用上面創建的控制器來訪問JSON文件,我使用鏈接localhost/project/list.json。但是,如果我要使用JSON構建器,我將如何提供到json文件的鏈接。 – codeBarer
你的意思是像'json {render builder.toString()}'? –