JSON stands for JavaScript Object Notation. It is one of the widely used formats to exchange data by web applications. JSON arrays are almost the same as arrays in JavaScript. They can be understood as a collection of data (strings, numbers, booleans) in an indexed manner. Given a JSON array, we will discuss how to convert it into a String array in Java.
Creating a JSON array
Let’s start with creating a JSON array in Java. Here, we will use some example data to input into the array, but you can use the data as per your requirements.
1. Defining the array
JSONArray exampleArray = new JSONArray();
Note that we will import the org.json package in order to use this command. This is later discussed in the code.
2. Inserting data into the array
We now add some example data into the array.
exampleArray.put("Geeks "); exampleArray.put("For "); exampleArray.put("Geeks ");
Notice the space given after each string. This is being done here because when we will convert it into a String array, we want to ensure that there is space between each element.
Now that we have our JSON array ready, we can move forward to the next and final step, converting it into a String array.
Conversion into a String array
The approach that we are using here, will first insert all the JSON array elements into a List since it is then easier to convert List into an array.
1. Creating a List
Let’s start by creating a List.
List<String> exampleList = new ArrayList<String>();
2. Adding JSON array data into the List
We can loop through the JSON array to add all elements to our List.
for(int i=0; i< exampleArray.length; i++){ exampleList.add(exampleArray.getString(i)); }
Now we have all the elements inside our List as strings, and we can simply convert the List into a String array.
3. Getting String array as output
We will use toArray() method to convert the List into a String array.
int size = exampleList.size(); String[] stringArray = exampleList.toArray(new String[size]);
This will convert our JSON array into a String array. The code has been provided below for reference.
Implementation:
Java
// importing the packages import java.util.*; import org.json.*; public class GFG { public static void main(String[] args) { // Initialising a JSON example array JSONArray exampleArray = new JSONArray(); // Entering the data into the array exampleArray.put( "Geeks " ); exampleArray.put( "For " ); exampleArray.put( "Geeks " ); // Printing the contents of JSON example array System.out.print( "Given JSON array: " + exampleArray); System.out.print( "\n" ); // Creating example List and adding the data to it List<String> exampleList = new ArrayList<String>(); for ( int i = 0 ; i < exampleArray.length; i++) { exampleList.add(exampleArray.getString(i)); } // Creating String array as our // final required output int size = exampleList.size(); String[] stringArray = exampleList.toArray( new String[size]); // Printing the contents of String array System.out.print( "Output String array will be : " ); for (String s : stringArray) { System.out.print(s); } } } |
Output:
Given JSON array: ["Geeks ","For ","Geeks "] Output String array will be : Geeks For Geeks