I have an JSON file, in this JSON file convert into XML format using parser, and then write back to xml file
我有一个JSON文件,在这个JSON文件中使用解析器转换成XML格式,然后写回XML文件
I can't find any examples of how to do this in Groovy
在Groovy中,我找不到任何这样做的示例
If I have JSON looking like this:
如果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"
}
]
}
]}
Then I should convert it to the XML looking like this:
然后我应该把它转换成这样的XML:
<?xml version="1.0"?>
<sampleConfiguration version="1.0" description="SampleDesc">
<params>
<sampleParameter enabled="1" 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>
I have been looking for JSON to XML converting code
我一直在寻找JSON到XML转换代码
1 个解决方案
#1
5
If you make your JSON valid ("
round the names, and a comma in the initial block), you can do this to convert it (specifically crafted to this example)
如果您使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
There is no general case for converting xml to json that I know of.
据我所知,将xml转换成json并没有一般的理由。
The output from this example is:
这个例子的输出是:
<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>
#1
5
If you make your JSON valid ("
round the names, and a comma in the initial block), you can do this to convert it (specifically crafted to this example)
如果您使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
There is no general case for converting xml to json that I know of.
据我所知,将xml转换成json并没有一般的理由。
The output from this example is:
这个例子的输出是:
<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>