从JSON创建XML

时间:2014-12-07 17:01:12

标签: java xml json

我有一个JSON文件,我希望在JAVA中将其转换为XML,以使用即将出现的XML结构处理一些不同的东西。 我的文件看起来像这样:

{
    "published" : "2014-04-15T12:00:13.760Z",
    "actor" : {
        "objectType" : "person",
        "id" : "a00906a9-d3e3-40b5-8ef2-9e1051828c09",
        "displayName" : "Ashley Wessels"
    },
    "verb" : "floating_run",
    "object" : {
        "objectType" : "experiments",
        "id" : "e9d9f71a-6c9e-4724-98d1-32dee57bb015",
        "content" : [
            {
                "mass" : 10,
                "volume" : 392.2,
                "density" : 0.025497195308516064,
                "fluid" : 1
            },
            {
                "mass" : 150,
                "volume" : 150,
                "density" : 1,
                "fluid" : 1
            },
            {
                "mass" : 50,
                "volume" : 50,
                "density" : 1,
                "fluid" : 1
            },
            {
            "mass" : 50,
            "volume" : 100,
            "density" : 0.5,
            "fluid" : 1
            },
            {
            "mass" : 100,
            "volume" : 50,
            "density" : 2,
            "fluid" : 1
            },
            {
            "mass" : 400,
            "volume" : 300,
            "density" : 1.3333333333333333,
            "fluid" : 1
            }
        ]
    },
    "target" : {
        "objectType" : "experiment",
        "id" : "9383fbbe-e071-49b2-9770-46ddc4f8cd6e",
        "displayName" : "Grundel experiment"
    },
    "generator" : {
        "objectType" : "application",
        "url" : "splash",
        "id" : "04123e9e-14d0-447b-a851-805b9262d9a6",
        "displayName" : "splash"
    },
    "provider" : {
        "objectType" : "study",
        "url" : "http://go-lab.gw.utwente.nl/experiments/siswagrundel/",
        "id" : "0f8184db-53ba-4868-9208-896c3d7c25bb",
        "displayName" : "SiswaGrundel"
    },
    "publishedClient" : "2014-04-15T12:17:03.902Z",
    "publishedServer" : "2014-04-15T12:00:13.760Z",
    "_id" : "534d1f4ddd5532e74400019f"
}

我使用了org.json库,并尝试了一些其他库,如JETTISON,从这个人创建一个xml文件,但大多数`XML.toString()“方法都无法处理此文件中的JSONArray。它只会产生奇怪的引号输出等。 是否有可以处理此类JSON结构的库?如果不是:我应该如何处理这种文件类型?

**编辑**

我在互联网上看到了这个文件似乎和我的结构相同。但我的编码错了,有谁知道为什么?

这是:

{"menu": {
    "header": "SVG Viewer",
    "items": [
        {"id": "Open"},
        {"id": "OpenNew", "label": "Open New"},
        null,
        {"id": "ZoomIn", "label": "Zoom In"},
        {"id": "ZoomOut", "label": "Zoom Out"},
        {"id": "OriginalView", "label": "Original View"},
        null,
        {"id": "Quality"},
        {"id": "Pause"},
        {"id": "Mute"},
        null,
        {"id": "Find", "label": "Find..."},
        {"id": "FindAgain", "label": "Find Again"},
        {"id": "Copy"},
        {"id": "CopyAgain", "label": "Copy Again"},
        {"id": "CopySVG", "label": "Copy SVG"},
        {"id": "ViewSVG", "label": "View SVG"},
        {"id": "ViewSource", "label": "View Source"},
        {"id": "SaveAs", "label": "Save As"},
        null,
        {"id": "Help"},
        {"id": "About", "label": "About Adobe CVG Viewer..."}
    ]
}}

最初提供的是bei org.json。

1 个答案:

答案 0 :(得分:1)

xml节点未正确显示的原因是JSONArray的名称。 出于某种原因,如果我从"内容"重命名JSONArray,它会正确显示。到" _content"或其他什么。

对我来说,它看起来像是来自org.json的错误。

此处使用的代码:

    Scanner scan = new Scanner(new File(jsonFile));
    String jsonData = "";
    while (scan.hasNext()) {
        jsonData += scan.nextLine();// + "\n";
    }
    scan.close();

    JSONObject json = new JSONObject(jsonData);

    String xml = org.json.XML.toString(json);

    System.out.println(xml);

正如我之前提到的,只是JSONArray的重命名解决了这个问题。 重命名后,输出如下所示:

<actor><displayName>Ashley Wessels</displayName><id>a00906a9-d3e3-40b5-8ef2-9e1051828c09</id><objectType>person</objectType></actor><publishedClient>2014-04-15T12:17:03.902Z</publishedClient><provider><displayName>SiswaGrundel</displayName><id>0f8184db-53ba-4868-9208-896c3d7c25bb</id><url>http://go-lab.gw.utwente.nl/experiments/siswagrundel/</url><objectType>study</objectType></provider><verb>floating_run</verb><generator><displayName>splash</displayName><id>04123e9e-14d0-447b-a851-805b9262d9a6</id><url>splash</url><objectType>application</objectType></generator><publishedServer>2014-04-15T12:00:13.760Z</publishedServer><published>2014-04-15T12:00:13.760Z</published><_id>534d1f4ddd5532e74400019f</_id><object><id>e9d9f71a-6c9e-4724-98d1-32dee57bb015</id><_content><volume>392.2</volume><density>0.025497195308516064</density><mass>10</mass><fluid>1</fluid></_content><_content><volume>150</volume><density>1</density><mass>150</mass><fluid>1</fluid></_content><_content><volume>50</volume><density>1</density><mass>50</mass><fluid>1</fluid></_content><_content><volume>100</volume><density>0.5</density><mass>50</mass><fluid>1</fluid></_content><_content><volume>50</volume><density>2</density><mass>100</mass><fluid>1</fluid></_content><_content><volume>300</volume><density>1.3333333333333333</density><mass>400</mass><fluid>1</fluid></_content><objectType>experiments</objectType></object><target><displayName>Grundel experiment</displayName><id>9383fbbe-e071-49b2-9770-46ddc4f8cd6e</id><objectType>experiment</objectType></target>

在重命名之前,它看起来像这样:

<actor><displayName>Ashley Wessels</displayName><id>a00906a9-d3e3-40b5-8ef2-9e1051828c09</id><objectType>person</objectType></actor><publishedClient>2014-04-15T12:17:03.902Z</publishedClient><provider><displayName>SiswaGrundel</displayName><id>0f8184db-53ba-4868-9208-896c3d7c25bb</id><url>http://go-lab.gw.utwente.nl/experiments/siswagrundel/</url><objectType>study</objectType></provider><verb>floating_run</verb><generator><displayName>splash</displayName><id>04123e9e-14d0-447b-a851-805b9262d9a6</id><url>splash</url><objectType>application</objectType></generator><publishedServer>2014-04-15T12:00:13.760Z</publishedServer><published>2014-04-15T12:00:13.760Z</published><_id>534d1f4ddd5532e74400019f</_id><object><id>e9d9f71a-6c9e-4724-98d1-32dee57bb015</id>{&quot;volume&quot;:392.2,&quot;density&quot;:0.025497195308516064,&quot;mass&quot;:10,&quot;fluid&quot;:1}
{&quot;volume&quot;:150,&quot;density&quot;:1,&quot;mass&quot;:150,&quot;fluid&quot;:1}
{&quot;volume&quot;:50,&quot;density&quot;:1,&quot;mass&quot;:50,&quot;fluid&quot;:1}
{&quot;volume&quot;:100,&quot;density&quot;:0.5,&quot;mass&quot;:50,&quot;fluid&quot;:1}
{&quot;volume&quot;:50,&quot;density&quot;:2,&quot;mass&quot;:100,&quot;fluid&quot;:1}
{&quot;volume&quot;:300,&quot;density&quot;:1.3333333333333333,&quot;mass&quot;:400,&quot;fluid&quot;:1}<objectType>experiments</objectType></object><target><displayName>Grundel experiment</displayName><id>9383fbbe-e071-49b2-9770-46ddc4f8cd6e</id><objectType>experiment</objectType></target>