我试图从歌曲API中获取一些数据。我想得到的是歌曲中的标签。
这里是JSON:
{"track":
{"name":"Let It Happen",
"url":"http://www.last.fm/music/Tame+Impala/_/Let+It+Happen",
"duration":"469000",
"streamable":{"#text":"0","fulltrack":"0"},
"listeners":"150111",
"playcount":"758518",
"artist":{"name":"Tame Impala","mbid":"63aa26c3-d59b-4da4-84ac-716b54f1ef4d","url":"http://www.last.fm/music/Tame+Impala"},
"toptags":
{"tag":[
{"name":"indie","url":"http://www.last.fm/tag/indie"},
{"name":"psychedelic","url":"http://www.last.fm/tag/psychedelic"},
{"name":"Psychedelic Rock","url":"http://www.last.fm/tag/Psychedelic+Rock"},
{"name":"2015","url":"http://www.last.fm/tag/2015"},
{"name":"electronic","url":"http://www.last.fm/tag/electronic"}
]
},
"wiki":
{"published":"11 Mar 2015, 08:59","summary":"Let It Happen is the opening track and first song revealed from Tame Impala's upcoming third album, Currents. <a href=\"http://www.last.fm/music/Tame+Impala/_/Let+It+Happen\">Read more on Last.fm</a>.","content":"Let It Happen is the opening track and first song revealed from Tame Impala's upcoming third album, Currents. <a href=\"http://www.last.fm/music/Tame+Impala/_/Let+It+Happen\">Read more on Last.fm</a>. User-contributed text is available under the Creative Commons By-SA License; additional terms may apply."}
}
}
从我学到的知识,我使用这段代码来解析它
private static ArrayList<String> parseTagsAPI(final String response)
{
ArrayList<String> tagTemp = new ArrayList<String>();
try
{
String tag="", logtag="test, test2";
JSONObject jsonObject = new JSONObject(response);
if (jsonObject.has("track"))
{
JSONObject jsonTrack = jsonObject.getJSONObject("track");
Log.d("API (tag)", "track");
if (jsonObject.has("toptags"))
{
JSONArray jsonArray = jsonTrack.getJSONObject("toptags").getJSONArray("tag");
Log.d("API (tag)", "track > album > toptags > tag(array)");
for (int i = 0; i < jsonArray.length(); i++)
{
if (jsonArray.getJSONObject(i).has("name"))
{
JSONObject tagArray = jsonArray.getJSONObject(i).getJSONObject("name");
tag = tagArray.optString("name");
logtag += (tag + ", ");
Log.d("API (tag)", "tags : " + tag);
}
tagTemp.add(tag);
}
}
}
Log.d("API (tag)", "Logtag : "+logtag);
} catch (Exception e) {
e.printStackTrace();
return new ArrayList<String>();
}
return tagTemp;
}
我放了一些日志来标记进度,但它只显示2个日志:
这意味着我的第二个if
无效。有人知道为什么会这样吗?
答案 0 :(得分:1)
You're looking for the wrong tag - it's just a typo.
private static ArrayList<String> parseTagsAPI(final String response)
{ ArrayList<String> tagTemp = new ArrayList<String>();
try
{ String tag="", logtag="test, test2";
JSONObject jsonObject = new JSONObject(response);
if (jsonObject.has("track"))
{ JSONObject jsonTrack = jsonObject.getJSONObject("track");
Log.d("API (tag)", "track");
if (jsonObject.has("toptags"))
{ JSONArray jsonArray = jsonTrack.getJSONObject("toptags").getJSONArray("tag");
Log.d("API (tag)", "track > album > toptags > tag(array)");
请注意,您正在寻找jsonObject.has("toptags")
而不是jsonTrack.has("toptags");
答案 1 :(得分:1)
这里错了:
JSONObject tagArray = jsonArray.getJSONObject(i).getJSONObject("name");
tag = tagArray.optString("name");
你应该这样做:
tag = jsonArray.getJSONObject(i).getString("name");
或
tag = jsonArray.getJSONObject(i).optString("name");
同样改变这个:
if (jsonObject.has("toptags"))
到
if (jsonTrack.has("toptags"))