Parsing JSON with GSON

Posted by Donn Felker on Stack Overflow See other posts from Stack Overflow or by Donn Felker
Published on 2010-04-25T21:56:30Z Indexed on 2010/04/25 22:03 UTC
Read the original article Hit count: 314

Filed under:
|
|

I'm having some trouble with GSON, mainly deserializing from JSON to a POJO.

I have the following JSON:

{
    "events": 
    [
        {
            "event": 
            {
                "id": 628374485, 
                "title": "Developing for the Windows Phone"
            }
        },
        {
            "event": 
            {
                "id": 765432, 
                "title": "Film Makers Meeting"
            }
        }
    ]
}

With the following POJO's ...

public class EventSearchResult {

    private List<EventSearchEvent> events; 

    public List<EventSearchEvent> getEvents() {
        return events;
    }

}
public class EventSearchEvent {

    private int id; 
    private String title;


    public int getId() {
        return id;
    }

    public String getTitle() {
        return title;
    }
}

... and I'm deserializing with the following code, where json input is the json above

Gson gson = new Gson();
return gson.fromJson(jsonInput, EventSearchResult.class);   

However, I cannot get the list of events to populate correctly. The title and id are always null. I'm sure I'm missing something, but I'm not sure what. Any idea?

Thanks

© Stack Overflow or respective owner

Related posts about gson

Related posts about JSON