为什么我从 JSON 变得无效 XML?

Why am I getting invalide XML from JSON?

这是我的代码(json 是一个字符串):

String xml = XML.toString(new JSONObject(json));

JSON:

{
    "components": {
        "header": "generic",
        "body": "generic",
        "footer": "watchlist"
    },
    "featuredArticle": {
        "title": "Test title",
        "text": "test text."
    },
    "header": {
        "ref": ""
    },
    "body": {
        "ref": ""
    },
    "footer": {
        "ref": ""
    }
}

XML:

<components>
    <footer>watchlist</footer>
    <header>generic</header>
    <body>generic</body>
</components>
<footer>
    <ref />
</footer>
<header>
    <ref />
</header>
<featuredArticle>
    <text>test text.</text>
    <title>Test title</title>
</featuredArticle>
<body>
    <ref />
</body>

当我验证 text mate 中的 JSON 时,它通过了,但 XML 没有。

在无效的 XML 标记语言中,您只需要一个包含元素的根元素,请在此处尝试:http://www.xmlvalidation.com/index.php?id=1&L=2 并验证 xml 是否正常,直到您关闭组件节点...

<root>
    all the xml content
</root>

一个有效的 XML 必须有一个 root element。你的例子没有。

尝试以下 JSON(添加根元素 "data"):

{
    "data": {
        "components": {
            "header": "generic",
            "body": "generic",
            "footer": "watchlist"
        },
        "featuredArticle": {
            "title": "Test title",
            "text": "test text."
        },
        "header": {
            "ref": ""
        },
        "body": {
            "ref": ""
        },
        "footer": {
            "ref": ""
        }
    }
}