Home >Java >javaTutorial >How can we convert JSONArray to String array in Java?
JSON is one of the widely used data exchange formats. It is a lightweight and languageindependent. JSONArray can parse text in a string to generate objects similar to vectors, and supports the java.util.List interface.
We can convert JSONArray to string array in the example belowExampleimport org.json.*; import java.util.*; public class JsonArraytoStringArrayTest { public static void main(String[] args) { JSONArray jsonArray = new JSONArray(); jsonArray.put("INDIA "); jsonArray.put("AUSTRALIA "); jsonArray.put("SOUTH AFRICA "); jsonArray.put("ENGLAND "); jsonArray.put("NEWZEALAND "); List<String> list = new ArrayList<String>(); for(int i=0; i < jsonArray.length(); i++) { list.add(jsonArray.getString(i)); } System.out.print("JSONArray: " + jsonArray); System.out.print("\n"); String[] stringArray = list.toArray(new String[list.size()]); System.out.print("String Array: "); for(String str : stringArray) { System.out.print(str); } } }
JSONArray: ["INDIA ","AUSTRALIA ","SOUTH AFRICA ","ENGLAND ","NEWZEALAND "] String Array: INDIA AUSTRALIA SOUTH AFRICA ENGLAND NEWZEALAND<strong> </strong>
The above is the detailed content of How can we convert JSONArray to String array in Java?. For more information, please follow other related articles on the PHP Chinese website!