Search code examples
javajsonjacksonjackson2

deserialize Json into POJO


I am trying to convert the following JSON structure (part of a larger JSON object) to a POJO but getting the exception copied below (using Java/Jackson).

JSON

"outputKeys":
{"ABC":"gGyIioUr4Jfr5QiCm6Z==",
"DEF":"RxHfNyD2JyPOpG5tv3Jaj5g=="}  

Java class

    private class OutputKeys {

        private String key;
        private String value;

        public OutputKeys(String key, String value) {
            this.key = key;
            this.value = value;
        }
}

&

ObjectMapper mapper = new ObjectMapper();
mapper.readValue(jsonString, Test.class);

exception:

no suitable constructor found, can not deserialize from Object value (missing default constructor or creator, or perhaps need to add/enable type information?

Test class has the OutputKeys as an attribute.
Any suggestions would be welcome. I have tried using a List of OutputKeys as well .

Update:

I have tried the following without success:

class OutputKeys {
public Map<String, String> keys;
///with constructor/setter/getters
}

&

class OutputKeys {
public List<OutputKey> keys;
///with constructor/setter/getters

   public class OutputKey {

        Map<String, String> outputs = new HashMap<>();
// tried this too:
// String key
//String value

}

Solution

  • Since the keys were dynamic, I ended up deserializing the data using the iterator on the JsonNode:

    jsonNode.get("outputKeys").iterator() 
    

    & then getting the relevant dynamic key information via the iterator.