... 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.