我在java中映射下一个json时遇到了一些问题:
{
"aggregations": {
"asMap": {
"detail": {
"name": "detail",
"value": 35100
},
"listed": {
"name": "listed",
"value": 618
}
}
},
"docCount": 9,
"key": "789028",
"keyAsNumber": 789028,
"keyAsText": {}
}
到下一个java bean:
public class RankingValue {
private Integer adId;
private Integer listed;
private Integer detail;
...
...
}
使用杰克逊进行地图是否可行?我想要的是获取内部详细信息的详细信息以及bean中列出的bean和值,最后是adId的键值。 有没有其他替代方案来映射这个?最好的方式?
提前致谢。
答案 0 :(得分:1)
我猜你必须使用自定义反序列化器。这似乎有效:
public class RankingValueDeserializer extends JsonDeserializer<RankingValue> {
@Override
public RankingValue deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
JsonNode node = jp.getCodec().readTree(jp);
Integer key = node.get("key").asInt();
Integer detail = node.get("aggregations").get("asMap").get("detail").get("value").asInt();
Integer listed = node.get("aggregations").get("asMap").get("listed").get("value").asInt();
return new RankingValue(key, detail, listed);
}
}
你的对象就像
@JsonDeserialize(using = RankingValueDeserializer.class)
public class RankingValue {
private Integer adId;
private Integer listed;
private Integer detail;
public RankingValue(Integer key, Integer listed, Integer detail) {
this.adId = key;
this.listed = listed;
this.detail = detail;
}
@Override
public String toString() {
return "RankingValue [adId=" + adId + ", listed=" + listed
+ ", detail=" + detail + "]";
}
}