Home >Java >javaTutorial >How can we convert JSONArray to String array in Java?

How can we convert JSONArray to String array in Java?

王林
王林forward
2023-08-26 15:45:132981browse

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 belowExample

import 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);
      }
   }
}

Output

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete