从嵌套的JSON创建列表视图

时间:2013-11-16 11:11:58

标签: java android json listview gson

我有一个JSON文件,我从互联网上获取,其中包含电视频道的日程安排数据。嵌套在该文件中,与大量元数据一起,是关于每个广播(即每个节目)的信息,下面是该文件的样本:

For ease of understanding, I recommend this visual representation of the below JSON instead (click on the Viewer tab on the JSON viewer).

{
"schedule": {
    "service": {
      "type": "tv",
      "key": "bbcnews",
      "title": "BBC News Channel"
    },
    "day": {
      "date": "2013-11-15",
      "has_next": 1,
      "has_previous": 1,
      "broadcasts": [
        {
          "is_repeat": false,   <=== This is the 1st broadcast programme
          "is_blanked": false,
          "pid": "p01ks4z3",
          "start": "2013-11-15T03:45:00Z",
          "end": "2013-11-15T04:00:00Z",
          "duration": 900,
          "programme": {
            "type": "episode",
            "pid": "b03hdhhp",
            "position": null,
            "title": "15/11/2013",
            "short_synopsis": "All the latest sports news and results from around the globe.",
            "media_type": "audio_video",
            "duration": 900,
            "display_titles": {
              "title": "Sport Today",
              "subtitle": "15/11/2013"
            },
            "first_broadcast_date": "2013-11-15T03:45:00Z",
            "ownership": {
              "service": {
                "type": "tv",
                "id": "bbc_news24",
                "key": "bbcnews",
                "title": "BBC News Channel"
              }
            },
            "programme": {
              "type": "brand",
              "pid": "b0121xvw",
              "title": "Sport Today",
              "position": null,
              "expected_child_count": null,
              "first_broadcast_date": "2011-06-13T02:45:00+01:00",
              "ownership": {
                "service": {
                  "type": "tv",
                  "id": "bbc_news24",
                  "key": "bbcnews",
                  "title": "BBC News Channel"
                }
              }
            },
            "is_available_mediaset_pc_sd": false,
            "is_legacy_media": false
          }
        },
        {
          "is_repeat": false,  <=== This is the 2nd broadcast programme
          "is_blanked": false,
          "pid": "p01ks4z4",
          "start": "2013-11-15T04:00:00Z",
          "end": "2013-11-15T04:30:00Z",
          "duration": 1800,
          "programme": {
            "type": "episode",
            "pid": "b03hdhhs",
            "position": null,
            "title": "15/11/2013",
            "short_synopsis": "Twenty-four hours a day, the latest national and international stories as they break.",
            "media_type": "audio_video",
            "duration": 1800,
            "display_titles": {
              "title": "BBC News",
              "subtitle": "15/11/2013"
            },
            "first_broadcast_date": "2013-11-15T04:00:00Z",
            "ownership": {
              "service": {
                "type": "tv",
                "id": "bbc_news24",
                "key": "bbcnews",
                "title": "BBC News Channel"
              }
            },
            "programme": {
              "type": "brand",
              "pid": "b006mgyl",
              "title": "BBC News",
              "position": null,
              "expected_child_count": null,
              "first_broadcast_date": "2006-11-01T13:00:00Z",
              "ownership": {
                "service": {
                  "type": "tv",
                  "id": "bbc_news24",
                  "key": "bbcnews",
                  "title": "BBC News Channel"
                }
              }
            },
            "is_available_mediaset_pc_sd": false,
            "is_legacy_media": false
          }
        }
      ]
    }
  }
}

使用the answer to this question on StackOverflow,我创建了一个类似的Javabean类:

private class ScheduleData {

private Schedule schedule;
// create getter & setter

public static class Schedule {
    private Service service;
    private Day day;
    // create getter & setter
}

public static class Service {
    private String type;
    private String key;
    private String title;
    // create getter & setter
}

public static class Day {
    private String date;
    private String has_next;
    private String has_previous;
    private Broadcasts broadcasts;
    // create getter & setter
}

public static class Broadcasts {
    private String is_repeat;
    private String is_blanked;
    private String pid;
    private String time;
    private String end;
    private String duration;
    private OuterProgramme programme;
    // create getter & setter
}

public static class OuterProgramme {
    private String type;
    private String pid;
    private String position;
    private String title;
    private String short_synopsis;
    private String media_type;
    private String duration;    
    private String first_broadcast_date;
    private DisplayTitles display_titles;
    private Ownership ownership;
    private InnerProgramme programme;
    // create getter & setter  
}

public static class DisplayTitles {
    private String title;
    private String subtitle;
    // create getter & setter
}

public static class Ownership {
    private Service service;
    // create getter & setter
}

public static class Service {
    private String type;
    private String id;
    private String key;
    private String title;
    // create getter & setter
}

public static class InnerProgramme {
    private String type;
    private String pid;
    private String title;
    private String position;
    private String expected_child_count;
    private String first_broadcast_date;
    private Ownership ownership;
    private String is_available_mediaset_pc_sd;
    private String is_legacy_media;
    // create getter & setter
}
}

在我的活动文件中,如何遍历获取的JSON的每个广播节点并检索short_synopsisdisplay_titles等程序数据并将其传递到自定义列表视图显示中?< /强>

1 个答案:

答案 0 :(得分:0)

1)定义根Json Wrapper类 ScheduleData.java

public class ScheduleData {
    private Schedule schedule;

    public Schedule getSchedule() {
        return schedule;
    }
}

2)将其属性定义为单独的公共类:

2.a) Schedule.java

public class Schedule {
    private Service service;
    private Day day;

    // TODO: create other getters & setters if you need
    public Day getDay() {
        return day;
    }
}

2.b) Service.java

public class Service {
    private String type;
    private String id;
    private String key;
    private String title;

    // TODO: create getters & setters if you need
}

2.c) Day.java

public class Day {
    private String date;
    private int has_next;
    private int has_previous;
    private Broadcast[] broadcasts;

    // TODO: create other getters & setters if you need
    public Broadcast[] getBroadcasts() {
        return broadcasts;
    }
}

2.d) Broadcast.java

public class Broadcast {
    private boolean is_repeat;
    private boolean is_blanked;
    private String pid;
    private String start;
    private String end;
    private int duration;
    private Programme programme;

    // TODO: create other getters & setters if you need
    public Programme getProgramme() {
        return programme;
    }
}

2.e) Programme.java

public class Programme {
    private String type;
    private String pid;
    private String position;
    private String title;
    private String short_synopsis;
    private String media_type;
    private int duration;
    private String first_broadcast_date;
    private DisplayTitle display_titles;
    private Ownership ownership;
    private Programme programme;

    // TODO: create other getters & setters if you need
    public String getShort_synopsis() {
        return short_synopsis;
    }

    public DisplayTitle getDisplay_titles() {
        return display_titles;
    }
}

2.f) DisplayTitle.java

public class DisplayTitle {
    private String title;
    private String subtitle;
    // create getter & setter
}

2.g) Ownership.java

public class Ownership {
    private Service service;
    // create getter & setter
}

3)定义AsyncTask并调用json服务。以流形式获取结果,并使用gson library将其值设置为ScheduleData实例。 (我假设您知道如何在Android上调用json服务,但如果不这样,则是5分钟的谷歌搜索问题。)

HttpEntity getResponseEntity = getResponse.getEntity();
InputStream source = getResponseEntity.getContent();

Gson gson = new Gson();
Reader reader = new InputStreamReader(source);
ScheduleData scheduleData = gson.fromJson(reader, ScheduleData.class);

4)现在您有ScheduleData个实例。它由服务的json响应填充。

Schedule schedule = scheduleData.getSchedule();
Day day = schedule.getDay();
Broadcast[] broadCastArr = day.getBroadcasts();
// TODO: use your broadCastArr in an adapter