废话不多说,直接上代码:
准备测试类:
public class Point {
private double x;
private double y;
public Point() {
x = Math.random();
y = Math.random();
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
}
Java类数组与json的相互转化:
public static void main(String[] args) {
Point[] test = new Point[3];
for (int i = 0; i < test.length; i++) {
test[i] = new Point();
}
String va = JSON.toJSONString(test);
System.out.println(va);
// Point[] ta = (Point[]) (JSON.parseArray(va, Point.class)).toArray(); // 编译通过,运行不灵
List<Point> tempa = JSON.parseArray(va, Point.class);
Point[] ta = tempa.toArray(new Point[0]); // 编译通过,运行不灵
for (int i = 0; i < ta.length; i++) {
System.out.println(JSON.toJSONString(ta[i]));
}
}