Jsonarray to string array – Java Program to Convert JSON Array to String Array

Jsonarray to string array: In the previous article, we have seen Java Program to Print Multiple Types of Arrays Using Method Overloading

In this article we are going to see how to Convert JSON array to String array.

Java Program to Convert JSON Array to String Array

Array is a data structure which stores a fixed size sequential collection of values of single type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.

In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.

Declaration of an array:

dataType[] arrayName; (or)                              //Declaring an array
dataType []arrayName; (or)
dataType arr[];

Instantiation of an Array:

arrayName = new datatype[size];                    //Allocating memory to array

Combining both Statements in One:

dataType[] arrayName = new dataType[size] //Declaring and Instantiating array

Initialization of an Array:

arrayName[index-0]= arrayElement1             //Initializing the array

...

arrayName[index-s]= arrayElementS

Combining all Statements in One:

dataType arrayName[ ]={e1,e2,e3};               //declaration, instantiation and initialization

Let’s see different ways how to to Convert JSON array to String array.

Method-1: Java Program to Convert JSON Array to String Array By Static Initialization of Array Elements

Approach:

  • Loop over the JSON array.
  • For each element in the JSON array insert it to the String array after converting it to string using toString() method.

Program:

import java.util.*;
import org.json.*;

public class Main 
{
    public static void main(String[] args) 
    {
        //JSON array created
        JSONArray jsonArray = new JSONArray();
        //Adding elements to JSON array
        jsonArray.add("a");
        jsonArray.add("b");
        jsonArray.add("c");
        //calling the user defined method convertJSONArraytoStringArray()
        convertJSONArraytoStringArray(jsonArray);
    }
    
    //convertJSONArraytoStringArray() converts JSON array to String array
    public public static void convertJSONArraytoStringArray(JSONArray[] jsonArray) 
    {
        String[] stringArray = new String[jsonArray.length];
        for (int i = 0; i < jsonArray.length; i++) 
        {
            //converting to string array by using toString() method
            stringArray[i] = jsonArray[i].toString();
        }

        System.out.println("Given JSON array: " + jsonArray);
        System.out.println("String array: " + Arrays.toString(stringArray));
    }

}
Output:

Given JSON array: ["a ","b ","c "]
String array: ["a ","b ","c "]

Method-2: Java Program to Convert JSON Array to String Array By Dynamic Initialization of Array Elements

Approach:

  • Create scanner class object.
  • Ask the user for number of elements.
  • Ask the user for array elements.
  • Loop over the JSON array.
  • For each element in the JSON array insert it to the String array after converting it to string using toString() method.

Program:

import java.util.*;
import org.json.*;

public class Main 
{
    public static void main(String[] args) 
    {
        JSONArray jsonArray = new JSONArray();
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the number of elements in the array: ");
        int n = sc.nextInt();
        sc.nextLine(); // to consume the newline character
        System.out.println("Enter the elements: ");
        for (int i = 0; i < n; i++) {
            jsonArray.put(sc.nextLine());
        }

        convertJSONArraytoStringArray(jsonArray);
    }

    public public static void convertJSONArraytoStringArray(JSONArray[] jsonArray) 
    {
        String[] stringArray = new String[jsonArray.length];
        for (int i = 0; i < jsonArray.length; i++) 
        {
            stringArray[i] = jsonArray[i].toString();
        }

        System.out.println("Given JSON array: " + jsonArray);
        System.out.println("String array: " + Arrays.toString(stringArray));
    }

}
Output:

Enter the number of elements in the array: 4
Enter the elements:
Hello
World
In
JSON
Given JSON array: ["Hello ","World ","In", “JSON”]
String array: ["Hello ","World ","In", “JSON”]

Are you a job seeker and trying to find simple java programs for Interview? This would be the right choice for you, just tap on the link and start preparing the java programs covered to crack the interview.

Related Java Programs: