我有host, port, uri由第三方编码为固定长度数组的json’ed 元组数组:
[
["www1.example.com", "443", "/api/v1"],
["proxy.example.com", "8089", "/api/v4"]
]
我想使用json魔术来获取的实例列表
class Endpoint {
String host;
int port;
String uri;
}
请帮助我放置适当的注释,以使ObjectMapper发挥作用。
我无法控制传入的格式,而我所有的google’n都以答案来回答如何将适当的json对象(而非数组)数组映射到对象列表
public static void main(String[] args) throws IOException {
String input = "" +
"[\n" +
" [\"www1.example.com\", \"443\", \"/api/v1\"],\n" +
" [\"proxy.example.com\", \"8089\", \"/api/v4\"]\n" +
"]";
ObjectMapper om = new ObjectMapper();
List endpoints = om.readValue(input,
new TypeReference>() {});
System.out.println("endpoints = " + endpoints);
}
@JsonFormat(shape = JsonFormat.Shape.ARRAY)
static class Endpoint {
@JsonProperty() String host;
@JsonProperty() int port;
@JsonProperty() String uri;
@Override
public String toString() {
return "Endpoint{host='" + host + '\'' + ", port='" + port + '\'' + ", uri='" + uri + '\'' + '}';
}
}