Phương thức JSONArray.toJSONString(List)
Thư viện json-simple cung cấp phương thức tĩnh JSONArray.toJSONString(List) để chuyển đổi một java.util.List thành chuỗi JSON array.
Ví dụ
import java.util.*;
import org.json.simple.JSONArray;
public class ListToJsonArrayExample {
public static void main(String[] args) {
List<String> countries = new ArrayList<>();
countries.add("India");
countries.add("Australia");
countries.add("England");
countries.add("South Africa");
countries.add("West Indies");
countries.add("Newzealand");
// Chuyển đổi List sang JSON array string
String jsonStr = JSONArray.toJSONString(countries);
System.out.println(jsonStr);
}
}
Kết quả
["India","Australia","England","South Africa","West Indies","Newzealand"]
Lưu ý: json-simple khác với org.json hay Gson. Maven dependency: org.json:json-simple (hoặc com.googlecode.json-simple:json-simple).