Org.Json - CSV Examples
Org.Json - Cookie Examples
Org.Json - HTTP Header Examples
Org.Json - JSON Examples
Org.Json - Property Examples
Org.Json - XML Examples
Org.Json - Exception Handling
Org.Json - Useful Resources
Org.Json - XML Class
XML class provides static methods to convert a XML text into a JSONObject, and vice versa.
Following methods are covered in the example.
toJSONObject(String) − Converts a XML to JSONArray Object.
toString(JSONObject) − Gives a XML from a JSONObject Object.
Example - Converting XML to JSON Object
JsonDemo.java
package com.tutorialspoint;
import org.json.JSONObject;
import org.json.XML;
public class JsonDemo {
public static void main(String[] args) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("Name", "Robert");
jsonObject.put("ID", 1);
jsonObject.put("Fees", Double.valueOf(1000.21));
jsonObject.put("Active", Boolean.TRUE);
jsonObject.put("Details", JSONObject.NULL);
//Convert a JSONObject to XML
String xmlText = XML.toString(jsonObject);
System.out.println(xmlText);
}
}
Output
<Active>true</Active><Details>null</Details><ID>1</ID><Fees>1000.21</Fees><Name>Robert</Name>
Example - Converting JSON Object to XML
JsonDemo.java
package com.tutorialspoint;
import org.json.XML;
public class JsonDemo {
public static void main(String[] args) {
//Convert a JSONObject to XML
String xmlText = "<Active>true</Active><Details>null</Details>"
+"<ID>1</ID><Fees>1000.21</Fees><Name>Robert</Name>";
//Convert an XML to JSONObject
System.out.println(XML.toJSONObject(xmlText));
}
}
Output
{"Active":true,"Details":null,"ID":1,"Fees":1000.21,"Name":"Robert"}
Advertisements