我有以下 JSON:
{
"points": [88,223,88,223,91,223]
}
我需要使用Jackson
将其映射到我的以下类:
public MyClass {
public List<Point2D.Float> points;
}
我怎样才能做到这一点?
最佳答案
首先,使用您选择的 JSON 解析器(显然是 Jackson)解析 JSON。
其次,迭代列表/数组,一次 2 个值,例如
public static List<Point2D.Float> toPoints(List<Float> coords) {
List<Point2D.Float> points = new ArrayList<>();
for (Iterator<Float> iter = coords.iterator(); iter.hasNext(); )
points.add(new Point2D.Float(iter.next(), iter.next()));
return points;
}
测试
System.out.println(toPoints(List.of(88f, 223f, 88f, 223f, 91f, 223f)));
输出
[Point2D.Float[88.0, 223.0], Point2D.Float[88.0, 223.0], Point2D.Float[91.0, 223.0]]
关于java - 将包含对的 JSON 数组转换为点列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58840572/