Convert JSON into XML using Java
18,716
Your application is alright. You need to have a well formed JSON object.
Source Code
package algorithms;
import org.json.JSONObject;
import org.json.XML;
public class JsonToXML{
public static void main(String args[])
{
JSONObject json = new JSONObject("{name: JSON, integer: 1, double: 2.0, boolean: true, nested: { id: 42 }, array: [1, 2, 3]}");
String xml = XML.toString(json);
System.out.println(xml);
}
}
Check with the example above.
Output:
<boolean>true</boolean><array>1</array><array>2</array><array>3</array><double>2.0</double><name>JSON</name><integer>1</integer><nested><id>42</id></nested>
Comments
-
Rose over 1 year
I'm trying to convert JSON into XML. But I'm getting an error that org.json cannot be resolved. I have also imported the external jar file java-json.jar. Below is my java code:
import org.json.JSONObject; public class JsontoXML{ public static void main(String args[]) { String str ={'name':'JSON','integer':1,'double':2.0,'boolean':true,'nested' {'id':42},'array':[1,2,3]}"; JSONObject json = new JSONObject(str); String xml = XML.toString(json); System.out.println(xml); }
}