<dependency>
<groupId>com.alibabagroupId>
<artifactId>fastjsonartifactId>
<version>1.2.40version>
dependency>
String s = "[[22,23,23],[1,10,20]]";
//字符串转换成二维数组
Integer[][] parse = JSON.parseObject(s, Integer[][].class);
Integer[][] toStr= new Integer[][]{{1,2,3},{4,5,6}};
String s = JSON.toJSON(toStr).toString();
import com.fasterxml.jackson.databind.ObjectMapper;
字符串转数组:
// 创建ObjectMapper对象
ObjectMapper objectMapper = new ObjectMapper();
// 将JSON格式转换成二维数组
String[][] arrTags = objectMapper.readValue(s, String[][].class);
数组转字符串:
String s = "[[22,23,23],[1,10,20]]";
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.writeValueAsString(s)
两种方法对比:
我觉得JSON这个好用一点,obejctMapper的这个还需要try catch捕获异常
第一种方法参考ElevenVitaminC写的方法,点击链接查看