Conversion between List and Json, and between ListJson
List and Json conversion require six jar packages. If you do not reference these jar packages, you need to write a large amount of code, here, the conversion using the jar package is recorded temporarily.
The following figure shows the structure of the test demo. It is for reference only. Pay attention to the version of the jar package.
The code in Test. java is as follows:
Package com. listandjson; import java. util. arrayList; import java. util. list; import net. sf. json. JSONArray; public class Test {public static void main (String [] args) {List <String> list = new ArrayList <String> (); list. add ("abc"); list. add ("123"); // convert list to jsonString json = JSONArray. fromObject (list ). toString (); System. out. println (json); // run: ["abc", "123"] // convert json to list JSONArray jsonArray = JSONArray. fromObject (json); List <String> list2 = (List) JSONArray. toCollection (jsonArray); for (int I = 0; I <list2.size (); I ++) {System. out. println (list2.get (I); // run: abc // 123 }}}