【发布时间】:2014-01-25 22:08:49
【问题描述】:
我正在构建一个 Web 应用程序,我想在其中显示 iTunes 排名前 10 的歌曲和 iTunes 排名前 10 的专辑。
我已使用https://rss.itunes.apple.com/ 链接生成它并从 XML 更改为 json。
http://itunes.apple.com/au/rss/topsongs/limit=10/json
我从上面的链接获取 JSON。并且可以在 JSON Viewer 中查看
http://jsonviewer.stack.hu/#http://itunes.apple.com/au/rss/topsongs/limit=10/json
但是,我对如何读取 JSON 对象以获取所需字段感到困惑。 (条目>标题)
我能够到达条目并遍历数组以获取所有标题。但是我不确定如何获得标签。
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 中,我正在关注其中包括标签。
JSON 对象:{"label":"Happy (from \"Despicable Me 2\") - Pharrell Williams"}
JSON 对象:{"label":"Trumpets - Jason Derulo"}
JSON 对象:{"label":"Rude - MAGIC!"}
JSON 对象:{"label":"All of Me - John Legend"}
JSON 对象:{"label":"Free (feat. Emeli Sandé) - Rudimental"}
JSON 对象:{"label":"I See Fire - Ed Sheeran"}
JSON 对象:{"label":"Timber (feat. Ke$ha) - Pitbull"}
JSON 对象:{"label":"God Only Knows - MKTO"}
JSON 对象:{"label":"Like a Drum - Guy Sebastian"}
JSON 对象:{"label":"Hey Brother - Avicii"}
我的问题是如何获得没有大括号和单词标签的标题。 同样在 JSON 条目中:im:collection 具有三个对象(im:name、link 和 im:contentType)。如何获得它们。
提前感谢您的帮助。
【问题讨论】: