2

Here is my Json response

"postedevent": [
    {
        "status": "true",
        "event_id": "800",
        "num_of_image_event": "0",
        "title": "Testy",
        "photo": "http://54.200.110.49/checkplanner/img/upload/21310059819profile_image_1409303464798.png",
        "event_date": "2014-08-29",
        "fullDate": "Friday - August 29, 2014",
        "event_from": "12:00AM",
        "event_to": "12:15AM",
        "city": "Ahm",
        "state": "CA",
        "member_id": "471",
        "username": "Krishna Mohan",
        "pencil": "yes",
        "attend": "yes",
        "company": "Development"
    }
 ]

this is java class to get java objs from json response

public class PostedEvent {

String status;
int event_id;
int num_of_image_event;
String title;
String photo;
String event_date;
String fullDate;
String event_from;
String event_to;
String city;
String state;
String member_id;
String username;
String pencil;
String attend;
String company;


}


public class PostedEvnetsList 
{
     ArrayList<PostedEvent> postedevent;
}

And I am parsing in this way

InputStream is = WebResponse.getResponse(url);
ObjectMapper mapper = new ObjectMapper();
PostedEvnetsList mList = null;
mList = mapper.readValue(is,PostedEvnetsList.class);
eventList = mList.postedevent;

I am getting following parse exception

jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "status" (Class com.example.jsonproforexam.PostedEvent), not marked as ignorable

I have declared same fields as in json response then why I am geting this exception Please help

HemangNirmal
  • 621
  • 1
  • 8
  • 24

2 Answers2

4

Your fields of PostedEvent and the PostedEvent field of PostedEventsList are not accessible.

You must set them as public (not recommended) or provide public getters and setters for them POJO-style.

Then Jackson will be able to de-serialize and the error will go away.

Mena
  • 47,782
  • 11
  • 87
  • 106
  • thanks a lot you solved my error and saved my time and efforts and I want one more help. Can you please explain me why Jackson can not access fields except public and what are the de-serialize strategy of Jackson – HemangNirmal Aug 28 '14 at 13:12
  • 1
    @HemangNirmal you're welcome. Jackson can access non-public fields as long as you provide public accessor methods (i.e. getters/setters), which is the recommended way (public fields defile encapsulation). You can find additional information, and documentation on their [homepage](https://github.com/FasterXML/jackson). – Mena Aug 28 '14 at 13:32
1

You can use the JsonProperty annotation to specify the json key

Ex:

public class PostedEvent {

    @JsonProperty("status")
    String status;

    @JsonProperty("event_id")
    String eventId;

....
....

If you have missed some fields from json in your entity class, you can use @JsonIgnoreProperties annotation to ignore the unknown fields.

@JsonIgnoreProperties(ignoreUnknown = true)
public class PostedEvent {

...
Abhishek V
  • 12,488
  • 6
  • 51
  • 63
  • thanks I follow your suggestion and figured out that if we make non public fields ignorable then Jackson can access that fields by taking reference from json – HemangNirmal Aug 28 '14 at 18:50