I have to make java object from my json file that looks like this: { "colors": ["red", "green", "blue"], "isPrimary": true, "rgb": { "r": 255, "g": 0, "b": 0 } }
I created class with parametrs that describes my file
public class Color {
private List<String> colors;
private boolean isPrimary;
private HashMap<String,Integer> rgb = new HashMap<String,Integer>();
public Color(){};
public List<String> getColors() {
return colors;
}
public void setColors(List<String> colors) {
this.colors = colors;
}
public boolean isPrimary() {
return isPrimary;
}
public void setPrimary(boolean primary) {
isPrimary = primary;
}
public HashMap<String, Integer> getRgb() {
return rgb;
}
public void setRgb(HashMap<String, Integer> rgb) {
this.rgb = rgb;
}
@Override
public String toString() {
return "Color{" +
"colors=" + colors +
", isPrimary=" + isPrimary +
", rgb=" + rgb +
'}';
}
}
Main class:
public static void main(String[] args) throws JsonProcessingException {
String jsonStr = "{\r\n" +
" \"colors\": [\"red\", \"green\", \"blue\"],\r\n" +
" \"isPrimary\": true,\r\n" +
" \"rgb\": {\r\n" +
" \"r\": 255,\r\n" +
" \"g\": 0,\r\n" +
" \"b\": 0\r\n" +
" }\r\n" +
"}";
ObjectMapper objectMapper = new ObjectMapper();
Color color = objectMapper.readValue(jsonStr, Color.class);
System.out.println(color);
}
I was following instruction but Probably something is wrong
Exception in thread "main" com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "isPrimary" (class itstep.task_5.Color), not marked as ignorable (3 known properties: "colors", "primary", "rgb"])
at [Source: (String)"{
"colors": ["red", "green", "blue"],
"isPrimary": true,
"rgb": {
"r": 255,
"g": 0,
"b": 0
}
}";