[英]Jackson: Object Mapper annotations to deserialize a inner collection
[英]Parsing JSON Response Using Jackson Object Mapper with inner collection array of map
我有一个JSON响应如下
{
"event_date": "2014-10-10T19:18:04+05:30",
"company": "b2c",
"entity_id": 7,
"attributes": {
"id": 7,
"order_id": 43,
"status": "dispatched",
**"quantities": [
[{"idno1":2},{"idno2":4}]
]**
}
}
在这里,我没有任何线索将其隐藏到java对象中,因为它具有数量不同的键值对,即键值对是变化的,即第一个元素的id为idno1,值是2,另一个键的idid2是idno2,值是4,所以这两个键值都在变化,并且这在数组数量内,这又在我的json内部。
请指导我如何使用带有ObjectMapper的jackson API在Java REST API中实现此目标。
非常感谢您的支持!!!!
您确定您的四位数对象有两个方括号"quantities":[[{"idno1":2},{"idno2":4}]]
吗? 这意味着它是一个数组或地图数组。 然后您可以创建obejct
class Attribute{
int id;
@JsonProperty("order_id")
int orderId;
String status;
List<List<Map<String, Integer>>> quantities;
//Getters & Setters
}
class Event {
@JsonProperty("event_date")
Date eventDate;
String company;
@JsonProperty("entity_id")
int entityId;
Attribute attributes;
//Getters & Setters
}
现在,对于您的json,您可以像
String json = "{\"event_date\":\"2014-10-10T19:18:04+05:30\",\"company\":\"b2c\",\"entity_id\":7,\"attributes\":{\"id\":7,\"order_id\":43,\"status\":\"dispatched\",\"quantities\":[[{\"idno1\":2},{\"idno2\":4}]]}}";
ObjectMapper mapper = new ObjectMapper();
Event eventObj = mapper.readValue(json, Event.class);
System.out.println(eventObj);
如果您不希望使用多余的括号,则可以将数量对象更改为List<Map<String, Integer>> quantities
,甚至减少为Map<String, Integer> quantities
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.