0

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.

2 Answers 2

4

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
}
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.