I have the current issue that I receive a JSON response where I don't know the name of the key (in my example JSON named UnknownSubCategorieX
).
The keys are always different from each other.
Here a JSON example:
{
"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"
}
],
...
}
This function could be applied for the inner part
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
}
inner part:
{
"orderNumber": "120345",
"type": "sell",
"name": "ProductA"
},
{
"orderNumber": "134006",
"type": "sell",
"name": "ProductB"
},
{
"orderNumber": "134003",
"type": "sell",
"name": "ProductB"
}
class
@Data
public class OpenOrderPOJO {
private String orderNumber;
private String type;
private String name;
}
With the suggestion of Mạnh Quyết Nguyễn
@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);
}
}
and this execution
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
}
}
I get this exception
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
Please help me to deserialize the whole JSON response.
It could be for example a map ´HashMap´ where the key is
UnknownSubCategorie`
However I tried to implement it, I got an exception and have meanwhile really a headache here.
I am not fixed to JACKSON, if someone can only help with GSON I would join this :)