How to convert JSON Array to normal Java Array?

Java 8Object Oriented ProgrammingProgramming

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]
raja
Published on 09-Jan-2018 18:00:29
Advertisements