Reputation: 476
Here is the json which I want to parse
{
"success": true,
"status": 200,
"events": [
{
"event": {
"artist_id": 54,
"created_at": "2013-04-05T08:52:40Z",
"duration": 2,
"end_time": "2013-06-06T22:30:00Z",
"event_desc": "Singer, composer, lyrist and Asia’s much lauded star, Jay Chou, will cast his spell on us once again on 6, 7 & 8 June at the Singapore Indoor Stadium at 8pm.\r\nHis most recent album “Opus 12” - the name symbolizing his 12th Mandarin album has been widely received and he now follows up with his eagerly anticipated world tour “OPUS JAY 2013 WORLD TOUR” which lets fans see Jay Chou’s\r\naccumulation of hard work since his first album in 2000.\r\nJay Chou rules with his trademark “Chou Style” known for his unusual brand of cross-genre pop, mixing Chinese and Western R & B beats sealing his status as the King of Mandopop.\r\n12 years on, Jay Chou still has plenty to give through his songs which cover a variety of genres and themes. Loyal fans can dance to Jay’s signature raps as well croon along with him though his soulful ballads.\r\nThis concert promises to be a spectacular experience from start to finish. Singapore is also Jay Chou’s third concert stop after Beijing and Shanghai and we get to experience this exciting and moving stage performance earlier in the tour!\r\nMark your calendars for a spectacular concert weekend and let the countdown begin!",
"event_facebook_link": "http://www.facebook.com/events/490248764356139",
"event_link": "http://www.sistic.com.sg/portal/dt?dt.isPortletRequest=true&dt.action=process&dt.provider=PortletWindowProcessChannel&dt.windowProvider.targetPortletChannel=JSPTabContainer/sEventsCalendar/Event&dt.containerName=JSPTabContainer/sEventsCalendar&dt.windowPr",
"feature_small": false,
"featured_status": true,
"id": 51,
"image": {
"url": "/uploads/event/image/51/Event.jpg",
"ratina_big": {
"url": "/uploads/event/image/51/ratina_big_Event.jpg"
},
"ratina_small": {
"url": "/uploads/event/image/51/ratina_small_Event.jpg"
},
"thumb_big": {
"url": "/uploads/event/image/51/thumb_big_Event.jpg"
},
"thumb_small": {
"url": "/uploads/event/image/51/thumb_small_Event.jpg"
},
"cell_big": {
"url": "/uploads/event/image/51/cell_big_Event.jpg"
},
"cell_small": {
"url": "/uploads/event/image/51/cell_small_Event.jpg"
}
}
Class defined for parsing is below
public class FeaturedPageData {
@SerializedName("status")
public String status;
@SerializedName("success")
public String success;
@SerializedName("events")
public ArrayList<Event> events = new ArrayList<FeaturedPageData.Event>();
public static class Event {
public Event() {
}
@SerializedName("name")
public String name;
@SerializedName("end_time")
public String end_time;
public Images images;
}
public static class Images {
public String ratina_big;
public String ratina_small;
}
}
The function where I am calling Json to parse
Reader reader = new InputStreamReader(content);
GsonBuilder gsonBuilder = new GsonBuilder();
Gson gson = gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create();
FeaturedPageData posts = new FeaturedPageData();
posts = gson.fromJson(reader, FeaturedPageData.class);
content.close();
handlePostsList(posts);
It shows the value for "success" and "status" but arraylist showing null for name and end_time
Upvotes: 2
Views: 3524
Reputation: 199
next time you can use http://www.jsonschema2pojo.org/ best tool for this kind problem for me :) change ArrayList to List
Upvotes: 0
Reputation: 18751
As it has been already pointed out in other answer, the problem is that you are trying to parse the "events"
JSON element as an array of Event
, so Gson expects something like:
"events": [
{
"artist_id": 54,
"created_at": "2013-04-05T08:52:40Z",
"duration": 2,
...
},
...
]
But you actually have:
"events": [
{
"event": {
"artist_id": 54,
"created_at": "2013-04-05T08:52:40Z",
"duration": 2,
...
}
},
...
]
Note that there's an string "event"
that is not being included anywhere in your class model.
So you have basically 2 options:
1.- You can create another class, let's say EventItem
, with only one field:
Event event;
And change your FeaturedPageData
class with:
ArrayList<EventItem> events;
2.- You can use a Map
instead of a List
, so that in your FeaturedPageData
class you should have:
Map<String, Event> events;
In this way you're telling Gson that the content of the array "events"
is a number of pairs: string ("event"
) and an object Event
, which is exactly what you have in your JSON response...
I think this 2nd solution is much better!
Upvotes: 5
Reputation: 101
I think this is because you are not actually parsing an array of Event objects, but an array of objects containing a single field named "event" that points to the Event object.
"events": [
{
"event": {
You have not declared a class for this container-object, so it doesn't get parsed.
Either Create a class for the outer object, or modify your json-array so it contains the Event object-directly.
Upvotes: 1