In Java, how can we convert a list to a JSON array?
Sep 20, 2023 pm 01:29 PMJSON is a lightweight, text-based and language independent data exchange Format. JSON can represent two structured types, such as object and array. An object is an unordered collection of key/value pairs, and an array is an ordered sequence of values.
We can use the JSONArray.toJSONString() method to convert the list into a JSON array, which is a static JSONArray method , which converts the list to JSON text , and the result is a JSON array.
Syntaxpublic 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"); <strong> </strong> // 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"]
The above is the detailed content of In Java, how can we convert a list to a JSON array?. For more information, please follow other related articles on the PHP Chinese website!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Break or return from Java 8 stream forEach?
