Rendering JSON in grails

后端 未结 3 511
余生分开走
余生分开走 2021-02-02 13:06

I use the following code to render data in JSON format.

render(contentType:\"text/json\") {
    results = array {
        db.eachRow(query) { row ->
                  


        
相关标签:
3条回答
  • 2021-02-02 13:42

    Only off by a little bit :) Just need to change

    aMap.put("A", a)
    

    to be a collection or list, rather than a map. so something like

    def aList = []
    aList << a
    

    Will get you what you want!

    As a sidenote, there is a JSON converter in grails that will do that string building for you. Look into it here

    0 讨论(0)
  • 2021-02-02 13:46

    This should be enough to render a JSON from controller:

    render results as grails.converters.JSON
    
    0 讨论(0)
  • 2021-02-02 13:55

    The way the JSON object is being built is quite obscure. What I like doing to render JSON responses in grails is creating a map or list in groovy and then use the render method just to convert it to JSON.

    Doing the transformation of the rowResult's inside the render method makes it quite confusing, I'd rather go for something like this

    def results = db.rows(query).collect { rowResult ->
        b(rowResult.name, c, d) 
    }
    render(contentType: 'text/json') {[
        'results': results,
        'status': results ? "OK" : "Nothing present"
    ]}
    

    I think it's more readable, and even shorter. This snippet gets you the desired result: no objects inside the results array, just strings.

    Note the use of rows, which returns a list of RowResult's, eliminating the need to get it from the ResultSet's. The list is used to collect the transformed value a by calling b on each row's name. Collecting the elements doesn't imply creating a map (like in the { "A":"value1"} JSON you were getting), just the same @will-buck achieved with the << operator on a new, empty list.

    All we do with the render method is declaring the text/json content type and passing it a literal map containing the keys results and status, which you want to write to the response. The conditional operator is used to concisely determine the status. It could also be used like this, by means of the JSON converter @will-buck also mentioned:

    def responseData = [
        'results': results,
        'status': results ? "OK" : "Nothing present"
    ]
    render responseData as JSON
    
    0 讨论(0)
提交回复
热议问题