Java – Error with Object mapper reading JSON from file

jacksonjava

I am trying to get to grips with Jackson JSON parser and have found a good tutorial that explains marshalling and unmarshalling. The tutorial is here

http://java.dzone.com/tips/json-processing-using-jackson

Now I have entered all the code, and I have set up the getters and setters in the username and user class. Now I want to be able to create a User Object from the JSON file as it shows in the tutorial i.e.

User user = mapper.readValue(new File("/Users/adam/Documents/JSON/user.json"), User.class);

However this returns an error

Exception in thread "main" org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "firstName" (Class UserName), not marked as ignorable
at [Source: /Users/adam/Documents/JSON/JSON.json; line: 1, column: 40] (through reference chain: User["userName"]->UserName["firstName"])

I can create an object from user if I use the String instead like so

String userDataJSON = "{\"userId\":\"100\",\"userName\":{\"firstname\":\"K\",\"middlename\":\"Siva\",\"lastname\":\"Prasad\"},\"dob\":1300878089906}";

User user = mapper.readValue(userDataJSON, User.class);

But If I try

 JsonFactory jfactory = new JsonFactory();
        JsonParser jParser = jfactory.createJsonParser(new File(
        "/Users/adam/Documents/JSON/user.json"));

    User user = mapper.readValue(jParser, User.class);

I get the error I showed above.

But same thing. Would anytime be able to tell me what I am doing wrong?? Thanks in advance

The JSON in the file is

{
"userId":"1",
"userName":{
"firstName":"Katamreddy",
"middleName":"Siva",
"lastName":"PrasadReddy"
},
"dob":1331113476212
}

Best Answer

"firstName" != "firstname"

Check the case of your property.