Reputation: 843
In my app I've to parse this 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"
}
]
}
]
}
}
I wanted to create some objects to store data from this JSON file so I used the Gson library to make this parsing very easy. I'm having trouble to create objects by using this library, I created 4 objects:
Here's the code of the objects:
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;
}
So I made the connection (with AsyncHttpClient) to download the JSON file from a server and I started to parse it, but I'm having trouble. I post here the code snippet:
public void onSuccess(String json) {
super.onSuccess(json);
Gson decoder = new Gson();
Programs programs = decoder.fromJson(json, Programs.class);
Log.d("PROGRAMS", "" + programs.program);
}
Why I can't access to Episode and EpisodeData? Why when I try to log programs.program
it returns null?
I hope you can help me to find a solution for this issue Thank you
Upvotes: 0
Views: 208
Reputation: 1844
The problem is in the base object. It should not be Programs but another object that has a Programs attribute. Apart from that, the Programs object has an array of Program as attribute, not a single instance.
Base object:
public class JsonRootObject {
public Programs programs;
}
Programs object:
public class Programs implements Serializable {
private static final long serialVersionUID = 1L;
public Program []program;
}
Program object:
public class Program implements Serializable {
private static final long serialVersionUID = 1L;
public Episode[] episode;
}
Episode object:
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 is not needed, as Episode is the last level.
And finally you can parse it with:
Gson decoder = new Gson();
JsonRootObject programs = decoder.fromJson(json, JsonRootObject.class);
I added a toString method to Episode class to test the parser, and this is the result:
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]
Hope that helps :)
Upvotes: 1
Reputation: 491
I think You should add @@SerializedName("-pN") above declaration of field pN, and do this to other fields too.
Upvotes: 0