Reputation: 323
I have a JSON-file, which I want to convert to XML in JAVA to process some different things with the outcoming XML-structure. My file looks like this:
{
"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"
}
I used the org.json library and also tried some other libraries like JETTISON to create a xml-file from this guy but most of the `XML.toString()" methodd are not able to handle the JSONArray in this file. It just produces weird output of quotations marks etc. Is there a library which can handle this type of JSON structure? If not: how should I handle this file type?
** EDIT **
I have looked on the internet and found this file which seems to have the same structure as mine. But mine is encoded wrong, does anyone know why?
Here it is:
{"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..."}
]
}}
It was provided originally bei org.json.
Upvotes: 0
Views: 228
Reputation: 323
The reason why the xml node wasn't displayed right is the name of the JSONArray. For some reason it is displayed right, if I rename the JSONArray from "content" to "_content" or something else.
For me, it looks like it is a bug from org.json.
Code used here:
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);
Just the rename of the JSONArray solved the problem, as I mentioned before. After the rename, the output looks like this:
<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>
Before the rename it looked like this:
<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>{"volume":392.2,"density":0.025497195308516064,"mass":10,"fluid":1}
{"volume":150,"density":1,"mass":150,"fluid":1}
{"volume":50,"density":1,"mass":50,"fluid":1}
{"volume":100,"density":0.5,"mass":50,"fluid":1}
{"volume":50,"density":2,"mass":100,"fluid":1}
{"volume":300,"density":1.3333333333333333,"mass":400,"fluid":1}<objectType>experiments</objectType></object><target><displayName>Grundel experiment</displayName><id>9383fbbe-e071-49b2-9770-46ddc4f8cd6e</id><objectType>experiment</objectType></target>
Upvotes: 1