json与list之间的转换
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");
//list转成json
String json =JSONArray.fromObject(list).toString();
System.out.println(json);
//运行:["abc","123"]
//json转成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));
//运行:abc
// 123
}
}
}