gpt4 book ai didi

groovy - 捕获 : java. lang.StackOverflowError JsonBuilder 关闭

转载 作者:行者123 更新时间:2023-12-03 16:22:22 24 4
gpt4 key购买 nike

我一直在尝试读取 xml 文件并使用 groovy 的 JsonBuilder 将其转换为 json。问题是当我打印时

def builder = new JsonBuilder(jsonObject)
println builder.toPrettyString()

我被捕获了: java.lang.StackOverflowError
这是整个堆栈跟踪
Exception in thread "main" java.lang.StackOverflowError
at groovy.json.JsonOutput.writeObject(JsonOutput.java:259)
at groovy.json.JsonOutput.writeIterator(JsonOutput.java:442)
at groovy.json.JsonOutput.writeObject(JsonOutput.java:272)
at groovy.json.JsonOutput.writeIterator(JsonOutput.java:442)
at groovy.json.JsonOutput.writeObject(JsonOutput.java:272)
at groovy.json.JsonOutput.writeIterator(JsonOutput.java:442)
at groovy.json.JsonOutput.writeObject(JsonOutput.java:272)
at groovy.json.JsonOutput.writeIterator(JsonOutput.java:442)
at groovy.json.JsonOutput.writeObject(JsonOutput.java:272)
at groovy.json.JsonOutput.writeIterator(JsonOutput.java:442)
at groovy.json.JsonOutput.writeObject(JsonOutput.java:272)
at groovy.json.JsonOutput.writeIterator(JsonOutput.java:442)
at groovy.json.JsonOutput.writeObject(JsonOutput.java:272)
at groovy.json.JsonOutput.writeIterator(JsonOutput.java:442)

这里是代码。
package firstgroovyproject

import groovy.json.JsonBuilder

class XmlToJsonII {
static void main(def args){

def carRecords = '''

<records>
<car name='HSV Maloo' make='Holden' year='2006'>
<countries>
<country>
Austria
</country>
<country>
Spain
</country>
</countries>
<record type='speed'>Production Pickup Truck with speed of 271kph
</record>
</car>
<car name='P50' make='Peel' year='1962'>
<countries>
<country>
Monaco
</country>
</countries>
<record type='size'>Smallest Street-Legal Car at 99cm wide and 59 kg
in weight</record>
</car>
<car name='Royale' make='Bugatti' year='1931'>
<record type='price'>Most Valuable Car at $15 million</record>
<countries>
<country>
Italia
</country>
</countries>
</car>
<car name='Seat' make='Ibiza' year='1985'>
<record type='price'>barato</record>
<countries>
<country>
Spain
</country>
</countries>
</car>
</records>
'''


def xmlRecords = new XmlSlurper().parseText(carRecords)

def jsonObject = [:]
jsonObject.records = []
def records = jsonObject.records



xmlRecords.car.each {xmlCar ->
records.add([
countries:
xmlCar.countries.children().each{ country ->
println "country : ${country.text()}"
[country: country.text()]
},

])
}

def builder = new JsonBuilder(jsonObject)

println builder.toPrettyString()
//println builder.toString()
}
}

最佳答案

tl;博士:你的第二个(内)each应该是 collect反而。

真正的答案: each的返回值是原版Iterable调用它。在这种情况下,这将是表达式 xmlCar.countries.children() 定义的 XML 对象集合。 .由于该集合中的对象包含对它们自己父对象的引用,因此您的 JSON 构建会导致无限回归,从而导致堆栈溢出。

这不会发生在您第一次(外部)使用 each 时,因为您没有使用返回值。相反,您将添加到预先存在的列表 ( records )。

通过更改第二个(内部)eachcollect ,您仍在遍历 countries 的子代元素。然而,不是返回原始的 XML 子级( each 的结果),而是编译并返回形式为 [country:"country_string_from_xml"] 的映射列表。 ,这似乎是所需的行为。

困惑 eachcollect这是 Groovy 中一个常见的新手错误……这让我上周发生的事情变得更加痛苦。 :-)

关于groovy - 捕获 : java. lang.StackOverflowError JsonBuilder 关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26664953/

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