0

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:

  • EpisodeData in which I store this informations (pN, episodeNumber, date, time, channel and channelLogo)
  • Episode it's an array of EpisodeData
  • Program it's an array of Episode
  • Programs in which I store the array Program

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

8
  • You are not creating problem classes for fetching data. check this demo Commented Aug 6, 2013 at 7:58
  • The code you say it doesn't work... I tried on my galaxy S2 and it shows me a white activity Commented Aug 6, 2013 at 8:07
  • Episode should have as much attributes as you want to parse, named exactly as they come in the json. As they starts with a "-" you can't neither rename the class' attributtes nor parse them as they have different names than Gson is expecting. Try to replace -pN with pN, -episodeNumber with episodeNumber and so on before you call decoder.fromJson like this: json.replaceAll("-pN", "pN"); etc. Commented Aug 6, 2013 at 8:08
  • @AdriánRodríguez: I will try it in a second and I will tell you if it works or not just a moment Commented Aug 6, 2013 at 8:11
  • @AdriánRodríguez: same problem: I changed the JSON entry by deleting the the -. I saw that in JSON I've Programs and Program and in my java class I've programs and program so I edited my JSON so: Programs -> programs and Program -> program, but still the same, when I write this Log.d("PROGRAMS", "" + programs.program); it shows me null... Why? I've update my JSON in my question Commented Aug 6, 2013 at 8:21

2 Answers 2

1

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 :)

Sign up to request clarification or add additional context in comments.

Comments

0

I think You should add @@SerializedName("-pN") above declaration of field pN, and do this to other fields too.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.