0

I have a text file 'input.txt' which contains this text

{
  "product" : {
    "name" : "Pro-1",
    "category" : "A"
  }
}

and a class

public class  Product {
    @JsonProperty("name")
    public String name;
    @JsonProperty("category")
    public String category
    ...
    ...
}

I am using Jackson

Product p = mapper.readValue(new File("input.txt"), Product.class);

My class has no attribute named "product" and as a result exception occurs when mapping the json text to the product object. So, what will be a proper way ignore this "product" attribute when mapping to Product object from the text file?

anik_s
  • 243
  • 3
  • 18

1 Answers1

0

try something like this.

public class OuterClass{
    @JsonProperty("product")
    public Product product;
}

OuterClass outerObject = mapper.readValue(new File("input.txt"), OuterClass.class);

then you can use it outerObject.product

subash
  • 3,116
  • 3
  • 18
  • 22