-1

I am trying to parse a JSON using Jackson,here is my class

@JsonIgnoreProperties(ignoreUnknown = true)
public class Customer {
    private String name;
    
    public void setName(String n) {
        name = n;
    }
    public String getName() {
        return name;
    }
}

and the runner class

public class jsontoObj {

    public static void main(String args[]) throws IOException {

        ObjectMapper mapper = new ObjectMapper();
            String json = "{\n" +
                    "  \"customer\":\n" +
                    "  {\n" +
                    "    \"name\": \"John Doeyy\"\n" +
                    "  }\n" +
                    "}";

            Customer customer = mapper.readValue( json, Customer.class);
            System.out.println(customer.getName());
    }

}

the setName method is working but getting null in customer.getName(). I don't want to use moxy

richard937
  • 199
  • 2
  • 3
  • 11
  • You have an object within an object. The internal object matches your `Customer` but you are trying to parse the *external* object, which only has a `customer` attribute, not a `name` attribute. – RealSkeptic Jun 24 '20 at 13:41

1 Answers1

-1

A correct json according to your mapping would be

String json = "{ \"name\": \"John Doeyy\" }";

I removed the embeded "customer" object

Airy
  • 748
  • 2
  • 10
  • 27