Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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]
Advertisements