java中数组反序列化_json将数组反序列化为Java对象

我有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 + '\'' + '}';

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值