In this article we will see how to convert XML into JSON in Java ? Here we will use Java-JSON library to convert XML to JSON.
First add below JSON dependency in your pom.xml
<dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <version>20201115</version> </dependency>
Now lets convert XML to JSON.
import org.json.JSONObject; import org.json.XML; public class XmlToJson { public static void main(String[] args) { String xmlStr = "<student>\r\n" + " <firstName>John</firstName>\r\n" + " <id>1</id>\r\n" + " <lastName>Smith</lastName>\r\n" + "</student>"; JSONObject json = XML.toJSONObject(xmlStr); System.out.println(json); } }
0 Comments