字符串:
[[{"x":5.552,"y":10.95},{"x":7.208,"y":10.95},{"x":7.208,"y":9.567},{"x":5.552,"y":9.567}]]
希望分割为一维数组:{5.552,10.95,7.208,10.95,7.208,9.567,5.552,9.567}
或者二维数组:{{5.552,10.95}{7.208,10.95}{7.208,9.567}{5.552,9.567}}
请问有什么方法可以做到吗?
字符串:
[[{"x":5.552,"y":10.95},{"x":7.208,"y":10.95},{"x":7.208,"y":9.567},{"x":5.552,"y":9.567}]]
希望分割为一维数组:{5.552,10.95,7.208,10.95,7.208,9.567,5.552,9.567}
或者二维数组:{{5.552,10.95}{7.208,10.95}{7.208,9.567}{5.552,9.567}}
请问有什么方法可以做到吗?
String st1r = "[[{'x':5.552,'y':10.95},{'x':7.208,'y':10.95},{'x':7.208,'y':9.567},{'x':5.552,'y':9.567}]]";
JSONArray jsonObject = JSON.parseArray(st1r);
List<Double> list = new ArrayList<>();
List<List<Double>> list2 = new ArrayList<>();
for (Object o : jsonObject) {
JSONArray arr = JSON.parseArray(JSON.toJSONString(o));
for (Object o1 : arr) {
List<Double> l = new ArrayList<>();
list.add(JSON.parseObject(JSON.toJSONString(o1)).getDouble("x"));
list.add(JSON.parseObject(JSON.toJSONString(o1)).getDouble("y"));
l.add(JSON.parseObject(JSON.toJSONString(o1)).getDouble("x"));
l.add(JSON.parseObject(JSON.toJSONString(o1)).getDouble("y"));
list2.add(l);
}
}
System.out.println(jsonObject);
System.out.println(list);
System.out.println(list2);
试一下,你自己把最后的结果转数组就行了