JSON एक हल्का डेटा-इंटरचेंज फॉर्मेट है और JSON का फॉर्मेट की-वैल्यू जोड़ी जैसा होता है। हम org.json.XML क्लास का उपयोग करके XML को JSON ऐरे में बदल सकते हैं, यह एक स्टैटिक मेथड, XML.toJSONObject() प्रदान करता है जो XML को JSON ऐरे में बदलता है।
सिंटैक्स
public static JSONObject toJSONObject(java.lang.String string) throws JSONException
नीचे दिए गए उदाहरण में, XML को JSON ऐरे में बदलना
उदाहरण
import org.json.*;
public class ConvertXMLToJSONArrayTest {
public static String xmlString = "<?xml version=\"1.0\" ?><root><test
attrib=\"jsontext1\">tutorialspoint</test><test attrib=\"jsontext2\">tutorix</test></root>";
public static void main(String[] args) {
try {
JSONObject json = XML.toJSONObject(xmlString); // XML को JSON में बदलता है
String jsonPrettyPrintString = json.toString(4); // JSON प्रिटी प्रिंट
System.out.println(jsonPrettyPrintString);
} catch(JSONException je) {
System.out.println(je.toString());
}
}
}
आउटपुट
{"root": {"test": [
{
"attrib": "jsontext1",
"content": "tutorialspoint"
},
{
"attrib": "jsontext2",
"content": "tutorix"
}
]}}