例如我有下一个域类
User{
String name
}
我也有这个类的 2 个对象
new User(name: "John").save()
new User(name: "Alex").save()
UserController 中的“列表”操作应该如何以这样的 JSON 格式表示 User.list()
{1: "John", 2: "Alex"}
让我说得更准确。我想要这样的东西:
UserController{
def list = {
render(contentType: "text/json") {
User.list().each {user->
user.id = user.name
}
}
}
但遗憾的是,这不起作用。
最佳答案
尝试数组结构,
def list = {
render(contentType: "text/json") {
results = array {
User.list().each {user->
result "${user.id}" : "${user.name}"
}
}
}
}
关于json - grails 将集合渲染为单个 json 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10898576/