The JSONis a lightweight, text-based and language-independentdata exchange format. The JSON can represent two structured types like objectsand arrays. An objectis an unordered collection of key/value pairs and an arrayis an ordered sequence of values.
We can convert a list to the JSON array using the JSONArray.toJSONString() method and it is a staticmethodof JSONArray, it will convert a list to JSON textand 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 list = new ArrayList(); 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"]Published on 19-Sep-2019 08:30:36