使用 groovy 将 JSON 转换为 XML? [英] Convert JSON to XML using groovy?

查看:34
本文介绍了使用 groovy 将 JSON 转换为 XML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an JSON file, in this JSON file convert into XML format using parser, and then write back to xml file

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

I can't find any examples of how to do this in 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:

Then I should convert it to the XML looking like this:

<?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 的转换代码

I have been looking for JSON to XML converting code

推荐答案

如果您使 JSON 有效(" 将名称四舍五入,并在初始块中使用逗号),您可以这样做转换它(专门为此示例制作)

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)

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 的一般情况.

There is no general case for converting xml to json that I know of.

这个例子的输出是:

<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>

这篇关于使用 groovy 将 JSON 转换为 XML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆