2013-08-06 89 views
0

在我的應用我已經解析這個JSON:解析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我在其中存儲該信息(PN,episodeNumber,日期,時間,渠道和channelLogo)
  • 情節這是一個EpisodeData
  • 計劃的陣列它的情節數組
  • 程序中,我存儲陣列計劃

這裏是對象的代碼:

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); 
} 

爲什麼我不能訪問到情節和EpisodeData?爲什麼當我嘗試登錄programs.program時,它返回null?

我希望你能幫助我找到這個問題 的解決方案謝謝

+0

您沒有爲獲取數據創建問題類。檢查[此演示](https://github.com/lalit3686/GSONDemo) –

+0

你說它的代碼不起作用...我試圖在我的星系S2,它顯示我一個白色的活動 – lucgian84

+0

情節應該儘可能多屬性就像你想要解析的一樣,按照它們進入json的方式命名。當他們以「 - 」開頭時,既不能重命名類,也不能解析它們,因爲它們的名稱與Gson期望的不同。嘗試在調用decoder.fromJson之前嘗試使用pN替換-pN,使用episodeNumber替換-episodeNumber等,然後再調用decoder.fromJson,如下所示:json.replaceAll(「 - pN」,「pN」);等等 –

回答

1

問題出在基礎對象上。它不應該是程序,而應該是具有「程序」屬性的另一個對象。除此之外,Programs對象還有一個Program作爲屬性的數組,而不是一個實例。

基礎對象:

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

我添加了一個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] 

希望幫助:)

+0

它的工作原理!非常感謝你! – lucgian84

0

我想你應該添加@@ SerializedName(「 - PN」)上述領域的PN聲明,這樣做是爲了其他領域太。

+0

我已經在我的問題中更新了我的JSON,看一看 – lucgian84