使用groovy将JSON转换为XML?

时间:2015-04-29 07:38:54

标签: xml json groovy

我有一个JSON文件,在这个JSON文件中使用解析器转换成XML格式,然后写回xml文件

我在Groovy中找不到任何如何执行此操作的示例

如果我的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"
            }
        ]
    }
]}

然后我应该将它转换为如下所示的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>

我一直在寻找JSON到XML转换代码

1 个答案:

答案 0 :(得分:5)

如果你使你的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>