Reputation: 49
I want to map following json to a pojo using jackson.
{
"colors": {
"red": {
"colorCode": "#FF0000"
},
"green": {
"colorCode": "#00FF00"
},
"blue": {
"colorCode": "#0000FF"
}
}
}
Is there any possible way to create a single POJO without having to create POJOs for each color because every color contains same parameter(colorCode)?
Note: I tried using @jsonAlias but it wont work because it overwrites that parameter.
Upvotes: 0
Views: 57
Reputation: 261
The name of the color is just the name. Unless blue Behaves differently then Red, then they should not be separate classes.
Public class Color{
Private string colorCode;
Private string title
Color(string colorCode, string title){
This.colorCode =colorCode;
This.title = title;
}
Getter setters etc
Upvotes: 0
Reputation: 130
The simplest solution would be to use a Map < String, Color > (or maybe Enum if you have a fixed list of colors)
public class Color{
private String colorCode;
//constructor, getter, setter
}
public class Pojo{
private Map<String,Color> colors;
//constructor, getter, setter
}
Upvotes: 4