Add jar dependencies to pom.xml

<dependency>
            <groupId>net.sf.json-lib</groupId>
            <artifactId>json-lib</artifactId>
            <version>0.9</version>
</dependency>
Copy the code

Second, get a JSON object in Java using the following method:

JSONArray js = JSONArray.fromObject(listName);
Copy the code

ToString (); toString();

String  result  =  js.toString();
Copy the code

The result is a json, simple, fast, but need to pay attention to understand the difference between the JSONObject and JSONArray Can back to how to use,

The code block

import net.sf.json.JSONArray; import java.util.ArrayList; public class test { public static void main(String[] args) { ArrayList<String> stringList = new ArrayList<String>(16); stringList.add("1"); stringList.add("2"); stringList.add("3"); stringList.add("4"); stringList.add("5"); stringList.add("6"); JSONArray jsonArray = JSONArray.fromArray(new ArrayList[]{stringList}); String listJson = jsonArray.toString(); System.out.println(listJson); }} Prints this: [["1","2","3","4","5","6"]]Copy the code

** Note: package import is correct, check API call **