从 Spotify 过滤元数据 API
Filter Metadata vom Spotify API
所以,我正在从 Spotify API 获取曲目和专辑的详细信息。现在我得到了巨大的回应。
我想要的只是艺术家、曲目、持续时间以及是否明确。我如何从那里得到 duration_ms 和显式?提前致谢:
响应看起来大约像这样:
{
"tracks": {
"href":
"items": [
"album":{...}
"artists":[..]
"duration_ms": 125666,
"explicit": false,
我现在已经尝试了各种变体,但它不起作用。这与我需要的元数据在数组中而不是 Json 正文中有关吗?
这是我现在所在的位置:
public static String filterMetadata(String metadata) throws IOException {
ObjectMapper mapper = new ObjectMapper();
JsonNode rootNode = mapper.readTree(metadata);
JsonNode durationNode = rootNode.at("/tracks/items/duration_ms"); //Path must start with a slash! /
String duration = durationNode.asText();
return duration;
}
编辑:谢谢你斯宾塞我现在这样做了:(我把变量从循环中取出来return它们回来了)
//Function to filter the response we got from Spotify back
public static String filterMetadata(String metadata) throws IOException {
JSONObject json = new JSONObject(metadata);
JSONObject tracks = json.getJSONObject("tracks");
JSONArray items = tracks.getJSONArray("items");
int duration_ms = 0;
boolean explicit = true;
for(int i = 0; i < items.length(); i++){
JSONObject item = items.getJSONObject(i);
duration_ms = item.getInt("duration_ms");
explicit = item.getBoolean("explicit");
}
您可以使用 org.json.JSONObject 来解析该响应。
//GET https://api.spotify.com/v1/albums/{id}/tracks
String response_json = "..." //from webservice
JSONObject json = new JSONObject(response_json);
JSONArray items = json.getJSONArray("items");
for(int i = 0; i < items.length(); i++){
JSONObject item = items.getJSONObject(i);
long duration_ms = item.getLong("duration_ms");
boolean explicit = item.getBoolean("explicit");
}
使用 Maven 时的依赖关系
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20180813</version>
<type>jar</type>
</dependency>
当然,这是为 https://developer.spotify.com/documentation/web-api/reference/#endpoint-get-an-albums-tracks, but you can paste any of the sample responses in an online json formatter (like https://jsonformatter.curiousconcept.com/ 处的获取专辑曲目响应量身定制的,因此您可以更好地可视化层次结构。
所以,我正在从 Spotify API 获取曲目和专辑的详细信息。现在我得到了巨大的回应。 我想要的只是艺术家、曲目、持续时间以及是否明确。我如何从那里得到 duration_ms 和显式?提前致谢:
响应看起来大约像这样:
{
"tracks": {
"href":
"items": [
"album":{...}
"artists":[..]
"duration_ms": 125666,
"explicit": false,
我现在已经尝试了各种变体,但它不起作用。这与我需要的元数据在数组中而不是 Json 正文中有关吗? 这是我现在所在的位置:
public static String filterMetadata(String metadata) throws IOException {
ObjectMapper mapper = new ObjectMapper();
JsonNode rootNode = mapper.readTree(metadata);
JsonNode durationNode = rootNode.at("/tracks/items/duration_ms"); //Path must start with a slash! /
String duration = durationNode.asText();
return duration;
}
编辑:谢谢你斯宾塞我现在这样做了:(我把变量从循环中取出来return它们回来了)
//Function to filter the response we got from Spotify back
public static String filterMetadata(String metadata) throws IOException {
JSONObject json = new JSONObject(metadata);
JSONObject tracks = json.getJSONObject("tracks");
JSONArray items = tracks.getJSONArray("items");
int duration_ms = 0;
boolean explicit = true;
for(int i = 0; i < items.length(); i++){
JSONObject item = items.getJSONObject(i);
duration_ms = item.getInt("duration_ms");
explicit = item.getBoolean("explicit");
}
您可以使用 org.json.JSONObject 来解析该响应。
//GET https://api.spotify.com/v1/albums/{id}/tracks
String response_json = "..." //from webservice
JSONObject json = new JSONObject(response_json);
JSONArray items = json.getJSONArray("items");
for(int i = 0; i < items.length(); i++){
JSONObject item = items.getJSONObject(i);
long duration_ms = item.getLong("duration_ms");
boolean explicit = item.getBoolean("explicit");
}
使用 Maven 时的依赖关系
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20180813</version>
<type>jar</type>
</dependency>
当然,这是为 https://developer.spotify.com/documentation/web-api/reference/#endpoint-get-an-albums-tracks, but you can paste any of the sample responses in an online json formatter (like https://jsonformatter.curiousconcept.com/ 处的获取专辑曲目响应量身定制的,因此您可以更好地可视化层次结构。