在我的应用程序中,我必须解析这个 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?
我希望你能帮助我找到解决这个问题的方法谢谢