1

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
  }
}";

2 Answers2

0

Your setter and getter have the wrong names. Jackson is trying to assing the value using setter methods in the format set{fieldname} such as setIsPrimary. But your setter is called setPrimary, and so the property "isPrimary" of your class (a property is identified by a getter and setter with the same name) cannot be found. That is what the Exception is trying to tell you.

TL;DR: rename your setPrimary method to setIsPrimary.

f1sh
  • 11,489
  • 3
  • 25
  • 51
0

The issue is that your JSON field name is isPrimary, but your Java field name is primary.

To fix this issue, you can either change the JSON field name to primary or add a @JsonProperty annotation to your Java field to specify that it should map to the isPrimary JSON field name:

import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.HashMap;
import java.util.List;

public class Color {
    private List<String> colors;
    @JsonProperty("isPrimary")
    private boolean primary;
    private HashMap<String, Integer> rgb = new HashMap<>();

    public Color() {}

    public List<String> getColors() {
        return colors;
    }

    public void setColors(List<String> colors) {
        this.colors = colors;
    }

    public boolean isPrimary() {
        return primary;
    }

    public void setPrimary(boolean primary) {
        this.primary = 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 +
                ", primary=" + primary +
                ", rgb=" + rgb +
                '}';
    }
}

import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;

public class Main {
    public static void main(String[] args) throws IOException {
        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);
    }
}

Output:

Color{colors=[red, green, blue], primary=true, rgb={r=255, b=0, g=0}}
Sash Sinha
  • 18,743
  • 3
  • 23
  • 40