问题
I have a method that returns fairly-nested JSON such as:
[[fizz: buzz, foos: [[count: 4, flim: flam], [count: 6, flim: flume]]]]
When I try to use JsonSlurper
to slurp this JSON into a def result
I am getting exceptions:
// json == “[[fizz: buzz, foos: [[count: 4, flim: flam], [count: 6, flim: flume]]]]"
String json = getJSON()
JsonSlurper slurper = new JsonSlurper()
def result = slurper.parseText(json)
Produces an exception thrown when parseText
executes:
Caught: groovy.json.JsonException: Unable to determine the current character, it is not a string, number, array, or object
Any ideas what the fix is?
回答1:
I think you're trying to use Groovy's map notation as JSON. JSON uses curlies for maps, like this
import groovy.json.*
def obj = [["fizz": "buzz", "foos": [["count": 4, "flim": "flam"], ["count": 6, "flim": "flume"]]]]
def json = JsonOutput.toJson(obj)
assert json == '''[{"fizz":"buzz","foos":[{"count":4,"flim":"flam"},{"count":6,"flim":"flume"}]}]'''
def result = new JsonSlurper().parseText(json)
来源:https://stackoverflow.com/questions/32231974/groovy-jsonslurper-and-nested-maps