Groovy JsonSlurper和嵌套地图 [英] Groovy JsonSlurper and nested maps

查看:185
本文介绍了Groovy JsonSlurper和嵌套地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个返回相当嵌套的JSON的方法,例如:

I have a method that returns fairly-nested JSON such as:

[[fizz: buzz, foos: [[count: 4, flim: flam], [count: 6, flim: flume]]]]

当我尝试使用JsonSlurper将此JSON插入到def result中时,出现异常:

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)

产生parseText执行时引发的异常:

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

有什么主意吗?

推荐答案

我认为您正在尝试将Groovy的地图符号用作JSON. JSON使用curly来绘制地图,像这样

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)

这篇关于Groovy JsonSlurper和嵌套地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆