3

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?

4

1 回答 1

4

我认为您正在尝试将 Groovy 的地图表示法用作 JSON。JSON 使用 curlies 作为地图,像这样

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)
于 2015-08-26T17:16:45.497 回答