4

I have problem when rendering List result, is grails can not render list? here my code

def findSome(){
    String query = params?.some
    List<Some> someList = Some.createCriteria().list(max : 5) {
        if(query != null && query != ""){
            and {
                like("name", query)
            }
        }

        order("name", "asc")
    }
    someList = someList == null ? new ArrayList<Some>() : someList

    ->> render someList as JSON
}

there is something wrong with my code? what I remember is, grails CAN render List of object. but with this code, always return null in line with mark ->>.

dadang1234
  • 65
  • 1
  • 10

4 Answers4

6

Since the result you obtain from your criteria query is a list of objects, you should use render someList as JSONArray. In order to render as JSON, your list should have corresponding key value pair. You better convert the someList to a map of key-value pairs and render as JSON if you really want JSON object.

   def findSome(){
String query = params?.some
List<Some> someList = Some.createCriteria().list(max : 5) {
    if(query != null && query != ""){
        and {
            like("name", query)
        }
    }

    order("name", "asc")
}
someList = someList == null ? new ArrayList<Some>() : someList

render someList as JSONArray

}

if you want to render as JSON use the following code:

  def findSome(){
def someMap=[:]
String query = params?.some
List<Some> someList = Some.createCriteria().list(max : 5) {
    if(query != null && query != ""){
        and {
            like("name", query)
        }
    }

    order("name", "asc")
}
someList = someList == null ? new ArrayList<Some>() : someList
 someList.each{
   someMap.put(it.id,it) 
 }
 render someMap as JSON

}

BenHuman
  • 175
  • 13
0

You should move the test for query outside of the criteria. No point creating it if query is nothing. You also don't need and in your criteria query, you can just use eq ("name", query)

In you're case the like and eq are the same.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • I have tried with what you said. with findAll() and I see the object is not null. but this controller always return null exception with there is `[` in error view. I have no idea what happen with this JSON converters :| – dadang1234 Oct 08 '17 at 00:39
0

Yes, grails can render object list. About your query, you could simplify to something like this.

def findSome() {
    respond Some.createCriteria().list {
        like 'name', "%$query%"

        maxResults 5
        order 'name', 'asc'
    }
}

If you are concerned that the query parameter is empty you can delegate that responsibility to command objects or url mappings constraints it depends on your case.

In the Responding with JSON section you can find other ways to respond to json

Mario
  • 4,784
  • 3
  • 34
  • 50
  • I think the error isn't at query. I have tried with findAll() and it return the same error :| – dadang1234 Oct 08 '17 at 00:40
  • What grails version do you use? – Mario Oct 08 '17 at 02:08
  • Grails Version: 3.2.8 – dadang1234 Oct 08 '17 at 14:21
  • you don need to depend on `render someList as JSON` you can try `render(contentType: 'application/json') { someList }` or `respond someList`, you can read about responding with JSON in this link http://docs.grails.org/latest/guide/single.html#jsonResponses. I updated my answer – Mario Oct 08 '17 at 14:27
  • I have tried it. its return null. why the list is not null, but when it render as json the list become null :| – dadang1234 Oct 08 '17 at 14:39
  • Could you share a real sample code? Maybe there is something not present in your sample – Mario Oct 08 '17 at 14:41
  • here the result where I render the object ```[com.Some : 1, com.Some : 3, com.Some : 5, com.Some : 2, com.Some : 4]``` but when render as JSON, it return null. – dadang1234 Oct 08 '17 at 14:54
0

That happened to me only once and I solved it by forcing the response:

render(text: someList as JSON, contentType: 'application/json', encoding: 'UTF-8')

It's quite obvious, but make sure the method is inside a controller and it has this configuration:

static responseFormats = ['json', 'html']
Ivan Pianetti
  • 709
  • 9
  • 13