如何漂亮地打印groovy.json.internal.LazyMap类型的JsonSlurper.parse(url)结果

如何漂亮地打印groovy.json.internal.LazyMap类型的JsonSlurper.parse(url)结果,json,groovy,Json,Groovy,我有一段调用GoogleGeocodeAPI并返回Json结果的代码,如下所示 def response = new JsonSlurper().parse (url.toURL()) 但是,返回类型实际上是groovy.json.internal.LazyMap类型 当我试着用下面的文字打印出来时 def res = JsonOutput.prettyPrint (response.toString()) 我犯了这样的错误 捕获:groovy.json.JsonExcep

我有一段调用GoogleGeocodeAPI并返回Json结果的代码,如下所示

    def response = new JsonSlurper().parse (url.toURL())
但是,返回类型实际上是groovy.json.internal.LazyMap类型

当我试着用下面的文字打印出来时

    def res = JsonOutput.prettyPrint (response.toString())
我犯了这样的错误

捕获:groovy.json.JsonException:Lexing在第1行第2列失败,读取“r”时,无法识别可能的有效json值或标点符号。
groovy.json.JsonException:Lexing在第1行第2列失败,读取“r”时,无法识别可能的有效json值或标点符号。
位于org.softwood.Geolocation.Geocoder.completeLatLong(Geocoder.groovy:29)
位于org.softwood.Geolocation.Geocoder$completeLatLong.call(未知来源)
位于org.softwood.Geolocation.TestGeoScript.run(TestGeoScript.groovy:13
惰性映射上的实际toString()给出了可能无法正确解析的原因,它不会在字符串结果周围加引号


{results=[{address\u components=[{long\u name=South Close,short\u name=South Cl,types=[location,political]},{long\u name=Ipswich,short\u name=Ipswich,types=[postal\u town]},{long\u name=Suffolk,short\u name=Suffolk,types=[administrative\u-area level 2,political]},{long_name=United Kingdom,short_name=GB,types=[country,political]},{long_name=IP42,short_name=IP42,types=[postal_code]},格式化地址=South Cl,Ipswich,Suffolk IP42,UK,geometry={bounds={North={lat=52.068566,lng=1.1667458},西南={lat=52.0672503,lng=1.1658643,位置=-lat=52.9999,lng=1691631},位置类型=几何中心,视口={东北={lat=52.0692571302915,lng=1.167654030291502},西南={lat=52.0665591697085,lng=1.164956069708498}},地点id=ChIJr3u-xXyf2UcRJF\U b9Yp2,类型=[路线],{地址组件=[{长名称=IP4 2H,短名称=IP4 2H,类型=[邮政编码],[短名称],[南名称]{=[route]},{long_name=Ipswich,short_name=Ipswich,types=[Location,political]},{long_name=Ipswich,short_name=Ipswich,types=[postal_town]},{long_name=Suffolk,short_name=Suffolk,types=[administrative_area_二级,political]},{long_name=英国,short_name=GB,types=[country,political]],格式化地址=South Cl,Ipswich,Suffolk IP4 2TH,UK,geometry={bounds={northeast={lat=52.068475,lng=1.1673588},southwest={lat=52.0666643,lng=1.1643497},location={lat=52.0676263,lng=1.1658643},位置类型=近似,视口={northeast={lat=52.0689186302915,lng=1.1673588},西南={lat=52.06767676263,lng=1.16586497},place_id=ChIJ7asZ3Xyf2UcRavs18W4IXUM,type=[邮政编码]},status=OK}


查询-给定返回的结果-如何将其“转换”为prettyPrint将解析和呈现的表单?

使用JsonBuilder似乎是最简单的方法:

String json = new JsonBuilder(response).toPrettyString()

应该给你想要的漂亮的json吗?

也可以使用
JsonOutput

import groovy.json.JsonOutput;

def json = JsonOutput.toJson([foo: 'bar', baz: [1]])
assert json == '{"foo":"bar","baz":[1]}'
def pretty = JsonOutput.prettyPrint(json)

该做的很好-这完成了要求的工作。可能会在GDK中提出一个请求,在groovy.json.internal.LazyMap上添加为方法。使用生成器put调用toPrettyString来重建json有点麻烦。我不能是唯一一个被这件事弄糊涂的人