How to convert JSON Array to normal Java Array?


The get method of the JSONArray class returns the element at a particular index. Using this method, you can get the elements of the JSONArray object and populate the array with them.

Example

import java.util.Arrays;
import org.json.JSONArray;

public class JsonToArray {
   public static void main(String args[]) throws Exception {
      String [] myArray = {"JavaFX", "HBase", "JOGL", "WebGL"};
      JSONArray jsArray = new JSONArray();
      for (int i = 0; i < myArray.length; i++) {
         jsArray.put(myArray[i]);
     }
     System.out.println(jsArray);
     String[] array = new String[myArray.length];
     for (int i = 0; i < myArray.length; i++) {
        array[i] = (String)jsArray.get(i);
     }
     System.out.println("Contents of the array :: "+Arrays.toString(array));
   }
}

Output

["JavaFX","HBase","JOGL","WebGL"]
Contents of the array :: [JavaFX, HBase, JOGL, WebGL]

Updated on: 19-Feb-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements