net.sf.json.JSONArray

Sep 21, 2018


目标

net.sf.json.JSONArray 转为 List
方法有两个:

  1. 先用 toCollection 转成 Collection 再强转为 List
  2. 直接用 toList 转成 List

数据

数据 data 如下:

[
	[1, "com.youdao.note", "有道云笔记", "6.3.2", "95"],
	[2, "com.baidu.browser.apps", "百度浏览器", "7.18.11.0", "253"]
]

toCollection

JSONArray array = JSONArray.fromObject(data);
Collection<List> collection = JSONArray.toCollection(array, List.class);
collection.forEach(a -> a.forEach(s -> System.out.println(s)));

强转

JSONArray array = JSONArray.fromObject(data);
List<List> list = (List<List>) JSONArray.toCollection(array, List.class);
list.forEach(a -> a.forEach(s -> System.out.println(s)));

toList

JSONArray array = JSONArray.fromObject(data);
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.setRootClass(String.class);
List<List> list = JSONArray.toList(array, List.class, jsonConfig);
list.forEach(a -> a.forEach(s -> System.out.println(s)));