Is there a way to use inbuilt Jackson capabilities to convert a list of json object to HashMap using java
Explanation: Json structure that i need to parse
{
list:[
{
keyId : 1,
keyLabel : "Test 1",
valueId: 34,
valueLabel: "Test Lable"
},
{
keyId : 2,
keyLabel : "Test 2",
valueId: 35,
valueLabel: "Test Lable"
},
{
keyId : 3,
keyLabel : "Test 3",
valueId: 36,
valueLabel: "Test Lable"
}
]
}
The object model I am expecting,
class Key{
int keyId;
String keyLable;
hashCode(){
return keyId.hashCode();
}
}
class Value{
int valueId;
String valueLable;
hashCode(){
return valueId.hashCode();
}
}
I need to convert the above json list to a map like this,
HashMap<Key,Value> map;
I would suggest to do it manually. You just have to write few lines. Something Like
ObjectMapper jmap = new ObjectMapper();
//Ignore value properties
List<Key> keys = jmap.readValue("[{}, {}]", jmap.getTypeFactory().constructCollectionType(ArrayList.class, Key.class));
//Ignore key properties
List<Value> values = jmap.readValue("[{}, {}]", jmap.getTypeFactory().constructCollectionType(ArrayList.class, Value.class));
Map<Key, Value> data = new HashMap<>();
for (int i = 0; i < keys.size(); i++) {
data.put(keys.get(i), values.get(i));
}
Note: There is spell mismatch in your json and model (valueLabel != valueLable).