Java 使用 Hutool 将 JSON 字符串数组转换为 List<List<String>>
可以使用以下代码实现:
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main(String[] args) {
String jsonStr = '[["a","b"],["c","d"]]';
JSONArray jsonArray = JSONUtil.parseArray(jsonStr);
List<List<String>> resultList = new ArrayList<>();
for (Object obj : jsonArray) {
JSONArray arr = (JSONArray) obj;
List<String> list = arr.toList(String.class);
resultList.add(list);
}
System.out.println(resultList);
}
}
首先,使用 JSONUtil.parseArray 将 JSON 字符串解析为 JSONArray 对象。
然后,遍历 JSONArray 中的每个元素,将其转换为 JSONArray 对象,并使用 toList 方法将其转换为 List
最后,将转换后的 List
输出结果为:[[a, b], [c, d]]
AI 优化版
原文地址:https://www.cveoy.top/t/topic/mUtU 著作权归作者所有。请勿转载和采集!