Note that there are some explanatory texts on larger screens.

plurals
  1. POParse JSON and create objects
    primarykey
    data
    text
    <p>In my app I've to parse this JSON:</p> <p><strong>programs.json</strong></p> <pre><code>{ "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" } ] } ] } } </code></pre> <p>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:</p> <ul> <li>EpisodeData in which I store this informations (pN, episodeNumber, date, time, channel and channelLogo)</li> <li>Episode it's an array of EpisodeData</li> <li>Program it's an array of Episode</li> <li>Programs in which I store the array Program</li> </ul> <p>Here's the code of the objects:</p> <p><strong>EpisodeData.java</strong></p> <pre><code>public class EpisodeData implements Serializable { /** * */ private static final long serialVersionUID = 1L; public String pN, episodeNumber, transmissionName, date, time, channel, channelLogo; } </code></pre> <p><strong>Episode.java</strong></p> <pre><code>public class Episode implements Serializable { /** * */ private static final long serialVersionUID = 1L; public EpisodeData[] episodeData; } </code></pre> <p><strong>Program.java</strong></p> <pre><code>public class Program implements Serializable { /** * */ private static final long serialVersionUID = 1L; public Episode[] episode; } </code></pre> <p><strong>Programs.java</strong></p> <pre><code>public class Programs implements Serializable { /** * */ private static final long serialVersionUID = 1L; public Program program; } </code></pre> <p>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:</p> <pre><code>public void onSuccess(String json) { super.onSuccess(json); Gson decoder = new Gson(); Programs programs = decoder.fromJson(json, Programs.class); Log.d("PROGRAMS", "" + programs.program); } </code></pre> <p>Why I can't access to Episode and EpisodeData? Why when I try to log <code>programs.program</code> it returns null?</p> <p>I hope you can help me to find a solution for this issue Thank you</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload