【问题标题】:Parsing with Gson giving null in nested array list在嵌套数组列表中使用 Gson 进行解析
【发布时间】:2013-05-22 12:46:11
【问题描述】:

这是我要解析的json

{

"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"
                }
            }

为解析定义的类如下

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;
}

}

我调用 Json 解析的函数

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);

它显示“成功”和“状态”的值,但数组列表显示名称和结束时间为空

【问题讨论】:

    标签: java android json parsing gson


    【解决方案1】:

    正如在其他答案中已经指出的那样,问题是您正试图将 "events" JSON 元素解析为 Event 的数组,因此 Gson 期望类似:

    "events": [
        {
            "artist_id": 54,
            "created_at": "2013-04-05T08:52:40Z",
            "duration": 2,
            ...
        },
        ...
    ]
    

    但你实际上有:

    "events": [
        {
            "event": {
                "artist_id": 54,
                "created_at": "2013-04-05T08:52:40Z",
                "duration": 2,
                ...
            }
        },
        ...
    ]
    

    请注意,有一个字符串 "event" 未包含在您的类模型的任何位置。


    所以你基本上有两个选择:

    1.-你可以创建另一个类,比如EventItem,只有一个字段:

    Event event;
    

    并更改您的 FeaturedPageData 类:

    ArrayList<EventItem> events;
    


    2.-您可以使用Map 而不是List,这样在您的FeaturedPageData 类中您应该有:

    Map<String, Event> events;
    

    通过这种方式,您告诉 Gson 数组 "events" 的内容是多个对:字符串 ("event") 和对象 Event,这正是您在 JSON 响应中所拥有的。 ..

    我认为第二个解决方案要好得多!

    【讨论】:

    • 感谢 Miko 的回答。我尝试了两种方法,但即使我创建另一类事件,它也会显示相同的错误,因为它显示预期的 begin_array 但在第 1 行第 41 列是 begin_object。
    • 对不起。它实际上起作用了,我又犯了一个愚蠢的错误。你是冠军兄弟。你拯救了我的一天。
    • @MikO 我认为您发布的第二个 JSON 存在小错误,因此我进行了编辑。那是你想要的代码吗?
    • @everton 是的,之前是错误的,您的编辑是有道理的,谢谢。
    【解决方案2】:

    我认为这是因为您实际上并没有解析 Event 对象数组,而是解析一个对象数组,其中包含一个名为“event”的字段,该字段指向 Event 对象。

    "events": [
        {
            "event": {
    

    你没有为这个容器对象声明一个类,所以它不会被解析。

    要么为外部对象创建一个类,要么修改您的 json-array 使其直接包含 Event 对象。

    【讨论】:

      【解决方案3】:

      下次你可以使用 http://www.jsonschema2pojo.org/ 最好的工具来解决我的此类问题:) 将 ArrayList 更改为 List

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多