0

在我的应用程序中,我必须解析这个 JSON:

程序.json

{
  "programs": {
    "program": [
      {
        "programNumber": "1",
        "imgURL": "http://www.photovideolife.com/userfiles/Placeholder%2001.jpg",
        "description": "Lorem ipsum dolor sit er elit",
        "episode": [
          {
            "pN": "1",
            "episodeNumber": "1",
            "transmissionName": "Titolo",
            "date": "29 Giu 2013",
            "time": "14:30",
            "channel": "Real Time",
            "channelLogo": "https://lh6.googleusercontent.com/-XSh-9ngYJu4/ThiY-xl2EeI/AAAAAAAABmc/iz04VpL5hOs/s800/realtime.png"
          },
          {
            "pN": "1",
            "episodeNumber": "1",
            "transmissionName": "Titolo",
            "date": "29 Giu 2013",
            "time": "16:30",
            "channel": "DMAX",
            "channelLogo": "http://tv.zam.it/canali/dmax.png"
          },
          {
            "pN": "1",
            "episodeNumber": "2",
            "transmissionName": "Titolo",
            "date": "01 Lug 2013",
            "time": "14:30",
            "channel": "Real Time",
            "channelLogo": "https://lh6.googleusercontent.com/-XSh-9ngYJu4/ThiY-xl2EeI/AAAAAAAABmc/iz04VpL5hOs/s800/realtime.png"
          },
          {
            "pN": "1",
            "episodeNumber": "2",
            "transmissionName": "Titolo",
            "date": "01 Lug 2013",
            "time": "16:30",
            "channel": "DMAX",
            "channelLogo": "http://tv.zam.it/canali/dmax.png"
          }
        ]
      },
      {
        "programNumber": "2",
        "imgURL": "http://mesa.umich.edu/files/mesa/field/image/placeholder2.png",
        "description": "Lorem ipsum dolor sit er elit",
        "Episode": [
          {
            "pN": "2",
            "episodeNumber": "1",
            "transmissionName": "Titolo 1",
            "date": "30 Giu 2013",
            "time": "13:30",
            "channel": "Real Time",
            "channelLogo": "https://lh6.googleusercontent.com/-XSh-9ngYJu4/ThiY-xl2EeI/AAAAAAAABmc/iz04VpL5hOs/s800/realtime.png"
          },
          {
            "pN": "2",
            "episodeNumber": "1",
            "transmissionName": "Titolo 1",
            "date": "30 Giu 2013",
            "time": "18:30",
            "channel": "DMAX",
            "channelLogo": "http://tv.zam.it/canali/dmax.png"
          },
          {
            "pN": "2",
            "episodeNumber": "2",
            "transmissionName": "Titolo 1",
            "date": "01 Lug 2013",
            "time": "13:30",
            "channel": "Real Time",
            "channelLogo": "https://lh6.googleusercontent.com/-XSh-9ngYJu4/ThiY-xl2EeI/AAAAAAAABmc/iz04VpL5hOs/s800/realtime.png"
          },
          {
            "pN": "2",
            "episodeNumber": "2",
            "transmissionName": "Titolo 1",
            "date": "01 Lug 2013",
            "time": "18:30",
            "channel": "DMAX",
            "channelLogo": "http://tv.zam.it/canali/dmax.png"
          }
        ]
      },
      {
        "programNumber": "3",
        "imgURL": "http://wp.contempographicdesign.com/wp_paramount/wp-content/themes/paramount/images/image_placeholder_lrg.jpg",
        "description": "Lorem ipsum dolor sit er elit",
        "Episode": [
          {
            "pN": "3",
            "episodeNumber": "1",
            "transmissionName": "Titolo 2",
            "date": "30 Giu 2013",
            "time": "10:30",
            "channel": "Real Time",
            "channelLogo": "https://lh6.googleusercontent.com/-XSh-9ngYJu4/ThiY-xl2EeI/AAAAAAAABmc/iz04VpL5hOs/s800/realtime.png"
          },
          {
            "pN": "3",
            "episodeNumber": "1",
            "transmissionName": "Titolo 2",
            "date": "30 Giu 2013",
            "time": "17:30",
            "channel": "DMAX",
            "channelLogo": "http://tv.zam.it/canali/dmax.png"
          },
          {
            "pN": "3",
            "episodeNumber": "2",
            "transmissionName": "Titolo 2",
            "date": "01 Lug 2013",
            "time": "10:30",
            "channel": "Real Time",
            "channelLogo": "https://lh6.googleusercontent.com/-XSh-9ngYJu4/ThiY-xl2EeI/AAAAAAAABmc/iz04VpL5hOs/s800/realtime.png"
          },
          {
            "pN": "3",
            "episodeNumber": "2",
            "transmissionName": "Titolo 2",
            "date": "01 Lug 2013",
            "time": "17:30",
            "channel": "DMAX",
            "channelLogo": "http://tv.zam.it/canali/dmax.png"
          }
        ]
      }
    ]
  }
}

我想创建一些对象来存储来自这个 JSON 文件的数据,所以我使用了 Gson 库来使这个解析变得非常容易。我无法使用此库创建对象,我创建了 4 个对象:

  • 我在其中存储此信息的 EpisodeData(pN、episodeNumber、日期、时间、频道和 channelLogo)
  • Episode 它是一个 EpisodeData 数组
  • 编程它是一系列剧集
  • 我存储数组的程序 Program

这是对象的代码:

EpisodeData.java

public class EpisodeData implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    public String pN, episodeNumber, transmissionName, date, time, channel, channelLogo;
}

情节.java

public class Episode implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    public EpisodeData[] episodeData;
}

程序.java

public class Program implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    public Episode[] episode; 
}

程序.java

public class Programs implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    public Program program;

}

因此,我建立了连接(使用 AsyncHttpClient)从服务器下载 JSON 文件并开始解析它,但我遇到了麻烦。我在这里发布代码片段:

public void onSuccess(String json) {
   super.onSuccess(json);
   Gson decoder = new Gson();
   Programs programs = decoder.fromJson(json, Programs.class);
   Log.d("PROGRAMS", "" + programs.program);
}

为什么我无法访问 Episode 和 EpisodeData?为什么当我尝试记录programs.program它时返回 null?

我希望你能帮助我找到解决这个问题的方法谢谢

4

2 回答 2

1

问题出在基础对象中。它不应该是 Programs,而是另一个具有 Programs 属性的对象。除此之外,Programs 对象有一个 Program 数组作为属性,而不是单个实例。

基础对象:

public class JsonRootObject {

    public Programs programs;

}

节目对象:

public class Programs implements Serializable {

    private static final long serialVersionUID = 1L;
    public Program []program;

}

程序对象:

public class Program implements Serializable {

    private static final long serialVersionUID = 1L;
    public Episode[] episode; 
}

剧集对象:

public class Episode implements Serializable {

    private static final long serialVersionUID = 1L;
    public String pN, episodeNumber, transmissionName, date, time, channel, channelLogo;
    @Override
    public String toString() {
        return "Episode [pN=" + pN + ", episodeNumber=" + episodeNumber
                + ", transmissionName=" + transmissionName + ", date="
                + date + ", time=" + time + ", channel=" + channel
                + ", channelLogo=" + channelLogo + "]";
    }
}

不需要 EpisodeData,因为 Episode 是最后一个级别。

最后你可以解析它:

Gson decoder = new Gson();
JsonRootObject programs = decoder.fromJson(json, JsonRootObject.class);

我在 Episode 类中添加了一个 toString 方法来测试解析器,结果如下:

System.out.println(programs.programs.program[0].episode[0]);

08-06 05:31:58.236: I/System.out(1249): Episode [pN=1, episodeNumber=1, transmissionName=Titolo, date=29 Giu 2013, time=14:30, channel=Real Time, channelLogo=https://lh6.googleusercontent.com/-XSh-9ngYJu4/ThiY-xl2EeI/AAAAAAAABmc/iz04VpL5hOs/s800/realtime.png]

希望有帮助:)

于 2013-08-06T09:33:40.923 回答
0

我认为您应该在字段 pN 的声明上方添加 @@SerializedName("-pN") ,并对其他字段也这样做。

于 2013-08-06T08:22:27.723 回答