
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How to convert XML to JSON array in Java?
A JSON is a lightweight data-interchange format and the format of JSON is like a key-value pair. We can convert XML to JSON array using org.json.XML class, this provides a static method, XML.toJSONObject() to convert XML to JSON array.
Syntax
public static JSONObject toJSONObject(java.lang.String string) throws JSONException
In the below example, converting XML to JSON array
Example
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); // converts xml to json String jsonPrettyPrintString = json.toString(4); // json pretty print System.out.println(jsonPrettyPrintString); } catch(JSONException je) { System.out.println(je.toString()); } } }
Output
{"root": {"test": [ { "attrib": "jsontext1", "content": "tutorialspoint" }, { "attrib": "jsontext2", "content": "tutorix" } ]}}
- Related Articles
- How to convert XML to Json and Json back to XML using Newtonsoft.json?
- Convert a JSON object to XML format in Java?\n
- How to convert a bean to XML using JSON-lib API in Java?
- How to convert Java Array/Collection to JSON array?
- How to convert JSON Array to normal Java Array?
- How to convert an array to JSON Array using JSON-lib API in Java?\n
- How to convert a JSON array to array using JSON-lib API in Java?\n
- How to convert a JSON array to CSV in Java?
- How to convert a bean to XML without type hints using JSON-lib API in Java?
- How to convert a Collection to JSON Array using JSON-lib API in Java?
- How to convert XML file into array in PHP?
- How can we convert a list to the JSON array in Java?
- How to convert a List to JSON array using the Jackson library in Java?
- How to convert JSON string to array of JSON objects using JavaScript?
- How to work with XML and JSON in .NET?

Advertisements