如果您使JSON有效(
"将名称四舍五入,并且在初始块中加一个逗号),则可以执行此 *** 作以将其转换(专门针对此示例)
def json = '''{ "name": "sampleConfiguration", "description": "SampleDesc", "version": "1.0", "parameters": [ { "name": "sampleParameter", "description": "parameter description", "value": "20", "enabled": "1" }, { "name": "items", "description": "parameter with subparameters", "value":[ { "name": "item", "description": "nested parameter", "value": "13" }, { "name": "item", "description": "nested parameter 2", "value": "TEST" } ] }]}'''import groovy.json.*import groovy.xml.*def xml = new JsonSlurper().parseText(json).with { j -> new StringWriter().with { sw -> new MarkupBuilder(sw)."$name"(version: version, description:description) { params { parameters.each { p -> if(p.value instanceof List) { "$p.name"(description:p.description) { p.value.each { v -> "$v.name"(description: v.description, v.value) } } } else { "$p.name"(description:p.description, p.value) } } } } sw.toString() }}println xml
我不知道将xml转换为json的一般情况。
该示例的输出为:
<sampleConfiguration version='1.0' description='SampleDesc'> <params> <sampleParameter description='parameter description'>20</sampleParameter> <items description='parameter with subparameters'> <item description='nested parameter'>13</item> <item description='nested parameter 2'>TEST</item> </items> </params></sampleConfiguration>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)