Render map as json in Grails

泄露秘密 提交于 2021-02-10 20:26:31

问题


I'm using Grails for my application, and I have a problem with JSON converters.

In my controller, I have:

def myObject = [:]
myObject.key = 'value'
render ???

what is optimal way to render myObject as JSON from the controller?


回答1:


It's easy :

render (myObject as JSON)

or

render ([key: "value"] as JSON)



回答2:


Other way is :

        render (contentType: 'text/json'){
        array {
            for(f in Person.list()){
                person id: f.id, name: f.name
            }
        }
    }

This sample render a list of Persons



来源:https://stackoverflow.com/questions/14457340/render-map-as-json-in-grails

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!