在我的應用我已經解析這個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?
我希望你能幫助我找到這個問題 的解決方案謝謝
您沒有爲獲取數據創建問題類。檢查[此演示](https://github.com/lalit3686/GSONDemo) –
你說它的代碼不起作用...我試圖在我的星系S2,它顯示我一個白色的活動 – lucgian84
情節應該儘可能多屬性就像你想要解析的一樣,按照它們進入json的方式命名。當他們以「 - 」開頭時,既不能重命名類,也不能解析它們,因爲它們的名稱與Gson期望的不同。嘗試在調用decoder.fromJson之前嘗試使用pN替換-pN,使用episodeNumber替換-episodeNumber等,然後再調用decoder.fromJson,如下所示:json.replaceAll(「 - pN」,「pN」);等等 –