使用java原生json(net.sf.json)包将list转json
许俊雅
2023-12-01
package json;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class JavaListToJsonList {
public static void main(String[] args) {
List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
for (int i = 0; i < 2; i++) {
Map<String,Object> map = new HashMap<String,Object>();
map.put("filePath", "svn://172.16.11.9/test/verisonManageTest/DEV/4.txt");
map.put("issueNum", "");
map.put("revison", i);
map.put("actions", "A");
list.add(map);
}
//因为list泛型为Map,所以要将map转为JSONObject
List list1 = new ArrayList();
for (int i = 0; i < list.size(); i++) {
JSONObject jsonObject = JSONObject.fromObject(list.get(i));
list1.add(jsonObject);
}
JSONArray fromObject = JSONArray.fromObject(list1);
System.out.println(fromObject);
}
}