2014-01-08 109 views
1

我正在構建一個Web應用程序,我想在其中顯示iTunes 10首歌曲和iTunes前10張專輯。我使用https://rss.itunes.apple.com/鏈接來生成它並從XML更改爲json。JSON中的iTunes RSS源 - 閱讀JAVA Webapps

http://itunes.apple.com/au/rss/topsongs/limit=10/json

我從上面的鏈接獲得JSON。並且可以在JSON查看器中查看

http://jsonviewer.stack.hu/#http://itunes.apple.com/au/rss/topsongs/limit=10/json

不過,我很迷惑於如何讀取JSON對象,這樣我可以得到所需的字段。 (entry> title)

我能夠進入條目並循環訪問數組以獲取所有標題。但是我不確定如何獲得標籤。

URL url = new URL("http://itunes.apple.com/au/rss/topsongs/limit=10/json"); 
     URLConnection connection = url.openConnection(); 

     String line; 
     StringBuilder builder = new StringBuilder(); 
     BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); 
     while((line = reader.readLine()) != null) { 
     builder.append(line); 
     } 

     JSONObject itunesJsonObject = new JSONObject(builder.toString()); 

     JSONObject feedJsonOject = itunesJsonObject.getJSONObject("feed");   
     JSONArray arrayJsonObject = feedJsonOject.getJSONArray("entry"); 

     List<String> list = new ArrayList<>(); 

     for(int i = 0 ; i < arrayJsonObject.length() ; i++){       

      list.add(arrayJsonObject.getJSONObject(i).getString("title"));    
     } 

     for(String e: list){ 
      log.debug("JSON Object : " + e.toString()); 
     } 

在log.debug中,我是下面包含標籤的geeting。

JSON對象:{ 「標記」: 「快樂(從\」 卑鄙的我2 \ 「) - 菲瑞威廉斯」}

JSON對象:{ 「標籤」: 「小號 - 詹森DERULO」}

JSON對象:{ 「標籤」: 「粗魯 - 神奇!」}

JSON對象:{ 「標籤」: 「我的全部 - 約翰傳奇」}

JSON對象:{ 「標籤」: 「免費(feat。EmeliSandé) - Rudimental」}

JSON對象:{「label」:「I See Fire - Ed Sheeran」}

JSON對象:{「label」:「Timber(feat。柯$公頃) - Pitbull的 「}

JSON對象:{」 標籤 「:」 只有上帝知道 - MKTO 「}

JSON對象:{」 標籤 「:」 如鼓 - 蓋伊塞巴斯蒂安「}

JSON對象:{「標籤」:「嘿,兄弟 - 航空工業第二集團公司」}

我的問題是如何得到的只是不花括號和文字標籤標題 此外,在JSON條目:IM:收藏有三個。對象(im:name,link和im:contentType)。如何獨立獲取它們。

感謝您的幫助提前。

回答

2

title是一個字段名爲label的對象,因此您必須將其解壓縮。像這樣的東西可能會奏效:

for(int i = 0 ; i < arrayJsonObject.length() ; i++){       
    list.add(arrayJsonObject.getJSONObject(i).getJSONObject("title").getString("label");    
} 

部分原始JSON:

"title": { 
    "label": "Happy (from \"Despicable Me 2\") - Pharrell Williams" 
}, 
+0

輝煌。工作完美。感謝你的回答。 –