
- 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 a JSON array to array using JSON-lib API in Java?n
The JSONArray is a sequence of values, the external text is a string enclosed in square brackets with commas separating the values and internal text is an object having get() and opt() methods, we need to access those values by index. The element() method for adding or replacing those values. An array is an object that stores multiple values of the same type. It can hold both primitive types and object references. We can convert a JSON array to array by using the toArray() method of JSONArray class. This method produces an Object[] with the contents of JSONArray.
Syntax
public Object[] toArray()
Example
import java.util.Arrays; import net.sf.json.JSONArray; public class ConvertJSONArrayToArrayTest { public static void main(String[] args) { JSONArray jsonArray = new JSONArray() .element("Raja Ramesh") .element("115") .element("Tutorials Point") .element("Hyderabad") .element(new String [] {"Java", "Testing", "Python"}); String jsonStr = jsonArray.toString(3); //pretty print JSON System.out.println("JSON:\n" + jsonStr); Object[] array = jsonArray.toArray(); System.out.println("-------------------------------------------------------------------"); System.out.println("Array:\n" + Arrays.toString(array)); } }
Output
JSON: [ "Raja Ramesh", "115", "Tutorials Point", "Hyderabad", [ "Java", "Testing", "Python" ] ] ---------------------------------------------------------------------------- Array: [Raja Ramesh, 115, Tutorials Point, Hyderabad, ["Java","Testing","Python"]]
- Related Articles
- How to convert a Collection to JSON Array using JSON-lib API in Java?
- How to convert an array to JSON Array using JSON-lib API in Java?\n
- How to convert a Map to JSON object using JSON-lib API in Java?
- How to convert a JSON string to a bean using JSON-lib API in Java?
- How to convert the JSON object to a bean using JSON-lib API in Java?
- How to convert a bean to XML using JSON-lib API in Java?
- How to add elements to JSON Object using JSON-lib API in Java?
- How to convert a bean to XML without type hints 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 JSON string to array of JSON objects using JavaScript?
- How to convert a JSON array to CSV in Java?
- How to convert XML to JSON array in Java?
- How can we implement a JSON array using Streaming API in Java?
- How to convert a List to JSON array using the Jackson library in Java?

Advertisements