Home > Java > javaTutorial > body text

How can we convert JSONArray to String array in Java?

王林
Release: 2023-08-26 15:45:13
forward
2883 people have browsed it

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);
      }
   }
}
Copy after login

Output

JSONArray: ["INDIA ","AUSTRALIA ","SOUTH AFRICA ","ENGLAND ","NEWZEALAND "]
String Array: INDIA AUSTRALIA SOUTH AFRICA ENGLAND NEWZEALAND<strong>
</strong>
Copy after login

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!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!