0

How can I deserialize multiple objects with same structure but different variable name using object mapper?

{ 
   "id":"A0D-29G3-03",
   "a":{ 
      "flag":"NORMAL",
      "date":"..."
   },
   "b":{ 
      "flag":"NORMAL",
      "date":"..."
   }
}

I'll have more objects than A and B. This is just an example. How can I deserialize multiple objects(using ObjectMapper) with same structure but different class name? Without creating one pojo for each class.....

user2354898
  • 253
  • 2
  • 15
  • 1
    This question is related to: [How can I deserialize a JSON to a Java class which has known mandatory fields, but can have several unknown fields?](https://stackoverflow.com/questions/57081709/how-can-i-deserialize-a-json-to-a-java-class-which-has-known-mandatory-fields-b), [JSON Jackson deserialization multiple keys into same field](https://stackoverflow.com/questions/57064917/json-jackson-deserialization-multiple-keys-into-same-field) – Michał Ziober Nov 18 '19 at 11:59

1 Answers1

2

... different class name

a and b are not different class names, are they? They are different fields which can share the same class. You can use one single class (Abc in the example) for fields a, b, etc.

Does this work for you?

    @Setter
    @Getter
    @ToString
    @JsonIgnoreProperties(ignoreUnknown = true)
    public static class AbcWrapper {
        private String id;
        @JsonAnySetter
        Map<String, Abc> abc = new LinkedHashMap<>();
    }

    @Setter
    @Getter
    @ToString
    public static class Abc {
        private String flag;
        private String date;
    }

    public static void main(String[] args) throws IOException {
        String json = "{ " +
                "   \"id\":\"A0D-29G3-03\"," +
                "   \"a\":{ " +
                "      \"flag\":\"NORMAL\"," +
                "      \"date\":\"...\"" +
                "   }," +
                "   \"a1\":{ " +
                "      \"flag\":\"NORMAL\"," +
                "      \"date\":\"...\"" +
                "   }," +
                "   \"a2\":{ " +
                "      \"flag\":\"NORMAL\"," +
                "      \"date\":\"...\"" +
                "   }," +
                "   \"a3\":{ " +
                "      \"flag\":\"NORMAL\"," +
                "      \"date\":\"...\"" +
                "   }," +
                "   \"b\":{ " +
                "      \"flag\":\"NORMAL\"," +
                "      \"date\":\"...\"" +
                "   }" +
                "}";


        ObjectMapper mapper = new ObjectMapper();
        final AbcWrapper abcWrapper = mapper.readValue(json.getBytes(), AbcWrapper.class);
        System.out.println(abcWrapper);
    }

I used lombok annotations @Setter, @Getter, @ToString in the example. You can replace them with setters/getters if you don't want to use lombok.

Petr Aleksandrov
  • 1,434
  • 9
  • 24
  • so what do you do if you have hundreds of `a`'s and `b`'s? the question is about that case – Vault23 Nov 18 '19 at 09:59
  • Yes you are correct. It's not different class names, it's different variable names. It's like Vault23 commented. What if I've many `a`'s and `b`'s? I want it to be dynamic – user2354898 Nov 18 '19 at 10:07
  • You can look at ```@JsonAnySetter```. I've modified the example in the answer. – Petr Aleksandrov Nov 18 '19 at 10:14
  • @ПетрАлександров yes, your answer is correct, `@JsonAnySetter` is a good way to use here. – Vault23 Nov 18 '19 at 10:21