java - 如何使用 RestTemplate 解析没有键的 json 值?
问题描述
我想解析 json 值,但有些值没有键。
像这样
"geometry": {
"type": "LineString",
"coordinates": [
[
127.08373748622218,
37.529508099979225
],
[
127.08355138558433,
37.529735847943925
]
]
}
几何类
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@ToString
public class Geometry {
private String type;
private Coordinate coordinates;
}
坐标类
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@ToString
public class Coordinate {
private List<Double[]> xy;
}
然后我看到了这个错误
Caused by: com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `kr.seoulmaas.ieye.service.dto.path.walk.Coordinate` out of START_ARRAY token
at [Source: (PushbackInputStream); line: 1, column: 133] (through reference chain: kr.seoulmaas.ieye.service.dto.path.WalkPathResDto["features"]->java.util.ArrayList[0]->kr.seoulmaas.ieye.service.dto.path.walk.Feature["geometry"]->kr.seoulmaas.ieye.service.dto.path.walk.Geometry["coordinates"])
解决方案
private Coordinate coordinates;
应该是private List<Coordinate> coordinates;
因为它是 JSON 中的一个数组,但由于该数组只包含数组,因此您需要一个列表列表:List<List<Double>> coordinates
(@JBNizet 建议)。
推荐阅读
- pip - Python 3.6:如何安装橡皮筋?
- c - 为什么我会从此结构中收到不兼容的指针类型警告?
- python - 你如何增加 matplotlib Line2D 艺术家的pickradius?
- java - 我正在尝试在 java 中打印一个链表数组,但出现“NullPointerException”错误
- azure - 集成 grafana 和 Azure 计费
- java - 如何使用变量来运行倒数计时器?
- ubuntu - 升级过程中无法重启 Ubuntu 18.04 中的 Samba
- quarkus - 忽略quarkus中的persistence.xml
- javascript - 如何将样式应用于 Material-Table 上的特定单元格?
- java - JSON 对象不正确并导致 HttpException