How to Convert JSON Array to String Array in Java?
Last Updated :
28 Mar, 2021
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
Similar Reads
How to Convert a String to ArrayList in Java?
Converting String to ArrayList means each character of the string is added as a separator character element in the ArrayList. Example: Input: 0001 Output: 0 0 0 1 Input: Geeks Output: G e e k s We can easily convert String to ArrayList in Java using the split() method and regular expression. Paramet
1 min read
How to Convert an ArrayList into a JSON String in Java?
In Java, an ArrayList is a resizable array implementation of the List interface. It implements the List interface and is the most commonly used implementation of List. In this article, we will learn how to convert an ArrayList into a JSON string in Java. Steps to convert an ArrayList into a JSON str
2 min read
How to Convert a String to a Character in Java?
String and char are fundamental and most commonly used datatypes in Java. A String is not a primitive data type like char. To convert a String to char in Java, we have to perform character-based operations or have to process individual characters.In this article, we will learn how to convert a Strin
3 min read
How to Convert Char to String in Java?
In this article, we will learn how to Convert Char to String in Java. If we have a char value like 'G' and we want to convert it into an equivalent String like "G" then we can do this by using any of the following three listed methods in Java. Using toString() method of Character classUsing valueOf(
5 min read
How to Convert an ArrayList of Objects to a JSON Array in Java?
In Java, an ArrayList is a resizable array implementation of the List interface. It implements the List interface and is the most commonly used implementation of List. In this article, we will learn how to convert an ArrayList of objects to a JSON array in Java. Steps to Convert an ArrayList of Obje
3 min read
How to Convert a String to URL in Java?
In Java, a string is a sequence of characters. A URL, which is short for Uniform Resource Locator, refers to a web resource that specifies its location on a computer network and provides the mechanism for retrieving it. To represent a URL in Java, we use the java.net.URL class. In this article, we w
2 min read
Convert a String to Character Array in Java
Converting a string to a character array is a common operation in Java. We convert string to char array in Java mostly for string manipulation, iteration, or other processing of characters. In this article, we will learn various ways to convert a string into a character array in Java with examples.E
2 min read
How to Convert a Vector of Objects into a JSON Array in Java?
In Java, the conversion of data structures like Vector to JSON (JavaScript Object Notation) format is required especially while working with the APIs or data interchange between different systems. The conversion of the Vector of an Object to the JSON Array has various approaches that we will learn i
3 min read
Java Program to Convert String to Integer Array
In Java, we cannot directly perform numeric operations on a String representing numbers. To handle numeric values, we first need to convert the string into an integer array. In this article, we will discuss different methods for converting a numeric string to an integer array in Java.Example:Below i
3 min read
How to Convert JSON Array Object to Java Object?
JSON arrays are useful for storing multiple values together that are related or part of the same set. For example, storing a list of items, user profiles, product catalog, etc. JSON arrays allow ordered access to the values using indices like in regular arrays in other languages (0 indexed). Convers
3 min read