当前有一个问题,我收到一个不知道密钥名称的JSON响应(在我的示例中,JSON名为$$
)。
键总是彼此不同的。
这是一个JSON示例:
UnknownSubCategorieX
此功能可以应用于内部
{
"UnknownSubCategorie1": [],
"UnknownSubCategorie2": [
{
"orderNumber": "120466",
"type": "sell",
"name": "ProductA"
},
{
"orderNumber": "120467",
"type": "sell",
"name": "ProductB"
}
],
"UnknownSubCategorie3": [
{
"orderNumber": "120345",
"type": "sell",
"name": "ProductA"
},
{
"orderNumber": "134006",
"type": "sell",
"name": "ProductB"
},
{
"orderNumber": "134003",
"type": "sell",
"name": "ProductB"
}
],
...
}
内部:
ObjectMapper mapper = new ObjectMapper();
List<OpenOrderPOJO> openOrderPOJOList = null;
try {
openOrderPOJOList = mapper.readValue(response, mapper.getTypeFactory().constructCollectionType(List.class, OpenOrderPOJO.class));
} catch (IOException e) {
e.printStackTrace();
}
if (openOrderPOJOList != null) {
...continue
}
班
{
"orderNumber": "120345",
"type": "sell",
"name": "ProductA"
},
{
"orderNumber": "134006",
"type": "sell",
"name": "ProductB"
},
{
"orderNumber": "134003",
"type": "sell",
"name": "ProductB"
}
在MạnhQuyếtNguyễn的建议下
@Data
public class OpenOrderPOJO {
private String orderNumber;
private String type;
private String name;
}
此执行
@Data
public class POJO {
private Map<String, List<OpenOrderPOJO>> unknownSubCategories;
@JsonAnySetter
public void setMap(String key, List<OpenOrderPOJO> value) {
if (unknownSubCategories == null) {
unknownSubCategories = new LinkedHashMap<>();
}
unknownSubCategories.put(key, value);
}
}
我得到这个例外
public static void main(String[] args) {
ObjectMapper mapper = new ObjectMapper();
List<POJO> openOrderPOJOList = null;
try {
openOrderPOJOList = mapper.readValue(JSONResponse, mapper.getTypeFactory().constructCollectionType(List.class, POJO.class));
} catch (IOException e) {
e.printStackTrace();
}
if (openOrderPOJOList != null) {
//continue
}
}
请帮助我反序列化整个JSON响应。
例如可能是地图“ HashMap”,其中com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.util.ArrayList` out of START_OBJECT token
at [Source: (StringReader); line: 1, column: 1]
at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:63)
at com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1342)
at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1138)
at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1092)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.handleNonArray(CollectionDeserializer.java:332)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:265)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:245)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:27)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4013)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3042)
at Poloniex.PrivateMethods.Main.main(Main.java:30)
Process finished with exit code 0
UnknownSubCategorie`
但是我尝试实现它,但是却遇到了一个异常,同时在这里真的很头疼。
我不只限于杰克逊,如果有人只能为GSON提供帮助,我会加入这个:)
答案 0 :(得分:2)
看起来您想使用未指定密钥的JSON映射(您的重复密钥示例可能会使这里的其他人感到困惑)。
要进行动态匹配,可以使用@JsonAnySetter
:
@Data
public class POJO {
private Map<String, List<OpenOrderPOJO>> unknownSubCategories;
@JsonAnySetter
public void setMap(String key, List<OpenOrderPOJO> value) {
if (unknownSubCategories == null) {
unknownSubCategories = new LinkedHashMap<>();
}
unknownSubCategories.put(key, value);
}
}
现在您可以像往常一样使用它与ObjectMapper反序列化
答案 1 :(得分:0)
只需将其添加到您的POJO类中,并为其获取和设置
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();