How to convert JSON array to normal Java Array Easily

By Saruque Ahamed Mollick

JSON data has become very much popular in the last few years. If you are a programmer or developer you know how much important is it to us. Even most of the API providers are now using JSON data to their users.
So in this Java tutorial post, I will show you how to convert JSON array to normal Java array.

In order to convert JSON array to string array in Java, you can use two methods.



  1. You can use normal String array of java
  2. Or you can use the ArrayList of Java (Working with ArrayList in Java ( Adding, Removing elements) )

Convert JSON array to normal Java Array using String array

It is very much simple to convert JSON array to string array in Java.

Main algorithm:

  • Fetch the length or size of the JSON array.
  • Create a String array in java.
  • Run a loop until it reaches its last element
  • Insert the JSON data to each element of the string array you have created before.

Please see this on https://www.tutorialspoint.com/How-to-convert-JSON-Array-to-normal-Java-Array

Convert JSON array to normal Java Array using ArrayList

Its much more easier way to do with ArrayList.

ArrayList<String> mylist = new ArrayList<String>();     
JSONArray this_is_jsonArray = (JSONArray)jsonObject; 
if (jsonArray == null) { 
System.out.println("json is empty");
}
else
{
   int length = this_is_jsonArray.length();
   for (int i=0;i<length;i++){ 
    mylist.add(this_is_jsonArray.get(i).toString());
   }  
}

I hope that this one makes you understand

Also read,

How to Modify Element or Elements of an ArrayList in Java

Working With LinkedList (Adding,Removing and Retrieving Elements) in Java

 

Leave a Reply

Your email address will not be published. Required fields are marked *