gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 19:35:16 27 4
gpt4 key购买 nike

我有一段代码调用 google geocode API 并返回 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_components=[{long_name=South Close, short_name=South Cl, types=[route]}, {long_name=Ipswich, short_name=Ipswich, types=[locality,political]}, {long_name=Ipswich, short_name =伊普斯维奇,types=[postal_town]},{long_name=Suffolk,short_name=Suffolk,types=[administrative_area_level_2,political]},{long_name=United Kingdom,short_name=GB,types=[country,political]},{long_name= IP4 2TH, short_name=IP4 2TH, types=[postal_code]}], formatted_address=South Cl, Ipswich, Suffolk IP4 2TH, UK, geometry={bounds={northeast={lat=52.068566, lng=1.1667458}, south={ lat=52.0672503, lng=1.1658643}}, location={lat=52.06789149999999, lng=1.1663008}, location_type=GEOMETRIC_CENTER, viewport={northeast={lat=52.0692571302915, lng=1.167654030291502}, southwest={lat=52.0665591697085, lng= 1.164956069708498}}},place_id=ChIJr3u-xXyf2UcRJF_b9Yp2_Ng,types=[route]},{address_components=[{long_name=IP4 2TH,short_name=IP4 2TH,types=[postal_code]},{long_name=South Close,short_name=South Cl , ty pes=[路线]},{long_name=Ipswich,short_name=Ipswich,types=[locality,political]},{long_name=Ipswich,short_name=Ipswich,types=[postal_town]},{long_name=Suffolk,short_name=Suffolk, types=[administrative_area_level_2,political]},{long_name=United Kingdom,short_name=GB,types=[country,political]}],formatted_address=South Cl,Ipswich,Suffolk IP4 2TH,UK,geometry={bounds={northeast= {lat=52.068475, lng=1.1673588}, 西南={lat=52.0666643, lng=1.1643497}}, location={lat=52.0676263, lng=1.1658643}, location_type=APPROXIMATE, viewport={northeast={lat=52.0689186302915, lng =1.1673588},西南={lat=52.0662206697085,lng=1.1643497}}},place_id=ChIJ7asZ3Xyf2UcRavs18W4IXUM,类型=[邮政编码]}],状态=OK}

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

最佳答案

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

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

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

关于json - 我如何漂亮地打印 groovy.json.internal.LazyMap 类型的 JsonSlurper.parse (url) 结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35576317/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com