小编典典

使用Groovy将JSON转换为XML?

json

我有一个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转换代码


阅读 625

收藏
2020-07-27

共1个答案

小编典典

如果您使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>
2020-07-27