jsonparsinggroovyjsonslurper

JsonSlurper returns No signature of method: groovy.json.JsonSlurper.parseText() is applicable for argument types: (java.util.ArrayList)


I'm trying to parse JSON file with JsonSlurper.parseText but keep getting similar problems.

def jsonParse = null
def http = new HTTPBuilder(url)
http.auth.basic(username, password)
http.request(Method.GET) {
    response.success = { resp, reader ->;
      jsonParse = new JsonSlurper().parseText(reader)
    }
}

Whenever I run my application the error message says

No signature of method: groovy.json.JsonSlurper.parseText() is applicable for argument types: (java.util.ArrayList)

I understand that JsonSlurper.parseText() is asking for a java.util.ArrayList type as an input. So I tried the following to figure out the type of the input using this code.

def jsonParse = null
def http = new HTTPBuilder(url)
http.auth.basic(username, password)
http.request(Method.GET) {
    response.success = { resp, reader ->;
      jsonParse = reader
    }
}
render jsonParse.getClass()

This prints out the following:

class java.util.ArrayList

I don't understand why I'm getting this error when I am feeding the input with correct datatype.

Any suggestions?


Solution

  • According to the documentation, the HTTPBuilder could be parsing your JSON for you. If your JSON response has its root as a JSON array, then that explains the ArrayList object in your reader variable.

    Regarding how this explains the exception being thrown. The reader parameter of the Closure is an ArrayList of parsed JSON, not a String of unparsed JSON. Thus, the code fails on new JsonSlurper().parseText(reader) because reader is not text and the JsonSlurper does not have a method defined for how to parse an ArrayList as JSON.