在我的应用程序中,我要解析这个JSON:
programs.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.java
public class EpisodeData implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
public String pN, episodeNumber, transmissionName, date, time, channel, channelLogo;
}
Episode.java
public class Episode implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
public EpisodeData[] episodeData;
}
Program.java
public class Program implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
public Episode[] episode;
}
Programs.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?
我希望你能帮我找到解决这个问题的方法 谢谢
答案 0 :(得分:1)
问题出在基础对象中。它不应该是程序,而是具有Programs属性的另一个对象。除此之外,Programs对象有一个Program as属性数组,而不是单个实例。
基础对象:
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]
希望有所帮助:)
答案 1 :(得分:0)
我认为您应该在字段pN的声明之上添加@@ SerializedName(“ - pN”),并对其他字段执行此操作。