The JSON is a lightweight, text-based and language-independent data exchange format. The JSON can represent two structured types like objects and arrays. An object is an unordered collection of key/value pairs and an array is an ordered sequence of values.
We can convert a list to the JSON array using the JSONArray.toJSONString() method and it is a static method of JSONArray, it will convert a list to JSON text and the result is a JSON array.
Syntax
public static java.lang.String toJSONString(java.util.List list)
Example
import java.util.*; import org.json.simple.*; public class ConvertListToJSONArrayTest { public static void main(String[] args) { List<String> list = new ArrayList<String>(); list.add("India"); list.add("Australia"); list.add("England"); list.add("South Africa"); list.add("West Indies"); list.add("Newzealand"); // this method converts a list to JSON Array String jsonStr = JSONArray.toJSONString(list); System.out.println(jsonStr); } }
Output
["India","Australia","England","South Africa","West Indies","Newzealand"]