4

我一直在尝试读取 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()
    }
}
4

1 回答 1

4

tl; dr:您的第二个(内部)each应该是 a collect

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

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

通过将第二个(内部)更改each为 a collect,您仍在迭代元素的子countries元素。但是,不是返回原始 XML 子项(的结果each),而是编译并返回表单的映射列表[country:"country_string_from_xml"],这似乎是所需的行为。

令人困惑each,并且collect是 Groovy 中一个常见的新手错误……当上周发生在我身上时,这让事情变得更加令人痛苦。:-)

于 2014-10-31T05:18:48.643 回答