私は以下のJSONオブジェクトを持っています(長さは申し訳ありません)、アーティストの名前を取得する必要があります。トラック - >アイテム - >アルバム - >アーティスト - >名前の下にある必要があります。このコードを実行しようとしていますが、アーティスト名を取得できません。ここではJSONオブジェクトの一部です:このJSONオブジェクトからアーティスト名の値を取得するにはどうすればよいですか?
{
"tracks" : {
"href" : "https://api.spotify.com/v1/search?query=closer&type=track&offset=0&limit=20",
"items" : [ {
"album" : {
"album_type" : "single",
"artists" : [ {
"external_urls" : {
"spotify" : "https://open.spotify.com/artist/69GGBxA162lTqCwzJG5jLp"
},
"href" : "https://api.spotify.com/v1/artists/69GGBxA162lTqCwzJG5jLp",
"id" : "69GGBxA162lTqCwzJG5jLp",
"name" : "The Chainsmokers",
"type" : "artist",
"uri" : "spotify:artist:69GGBxA162lTqCwzJG5jLp"
} ],
"available_markets" : [ "AD", "AR", "AT", "AU", "BE", "BG", "BO", "BR", "CA", "CH", "CL", "CO", "CR", "CY", "CZ", "DE", "DK", "DO", "EC", "EE", "ES", "FI", "FR", "GB", "GR", "GT", "HK", "HN", "HU", "ID", "IE", "IS", "IT", "JP", "LI", "LT", "LU", "LV", "MC", "MT", "MX", "MY", "NI", "NL", "NO", "NZ", "PA", "PE", "PH", "PL", "PT", "PY", "SE", "SG", "SK", "SV", "TR", "TW", "US", "UY" ],
.....
私はそれが"name" : "The Chainsmokers",
という行を取得する必要がありますが、私はそれを把握することはできません。これは私がこれまで持っているものです:私は曲のすべてを取得する必要があるため
JSONObject jObj;
JSONObject tracks;
JSONArray items;
JSONObject album;
JSONArray artists;
JSONObject aName;
jObj = new JSONObject(json);
tracks = (JSONObject) jObj.get("tracks");
items = (JSONArray) tracks.get("items");
String songName;
Log.d("searchSong", json);
// Return all of the results for the searched song. This will return to a ListView with the song name, uri, and artist name.
for (int i = 0; i < items.length(); i++) {
try {
songName = items.getJSONObject(i).getString("name");
if (!(songName.toLowerCase().contains(query.toLowerCase()))) {
continue;
}
// TODO THIS DOESN'T WORK!!!!
// How do I get artistname????
album = (JSONObject) items.getJSONObject(i).get("album");
artists = album.getJSONArray("artists"); // get the artist name
String artistsName = artists.getJSONObject(4).toString();
String artistName = "";
artistName = artists.getJSONObject(i).getString("name");
// This stuff works
id = items.getJSONObject(i).getString("id");
lViewSearch.setVisibility(View.VISIBLE);
lView.setVisibility(View.INVISIBLE);
bNext.setVisibility(View.INVISIBLE);
bPlay.setVisibility(View.INVISIBLE);
searchSongs.add(songName + "\n" + id);
searchAdapter.notifyDataSetChanged();
svSong.clearFocus();
} catch (JSONException e) {
e.printStackTrace();
}
} // end for loop
これは、forループであり、ここで私はそれだけの一部のためのJSONオブジェクトを掲載しました。何か問題があれば、教えてください、ありがとう!
それはすべてのエラーをスローしないべきか?代わりに何を得ていますか?標準的なデバッグの問題のようです。ブレークポイントを置くとあなたに役立ちます。また、 'strings ArtistsName = artists.getJSONObject(4).toString();'を使用すると、インデックス3を試してみるとよいでしょう。 – Sid
Spotify用のJava APIが見つかりませんか? –
私は長い間それを扱ってきましたが、未完成であり、スポットで作られていないので、私のために働くことは非常に難しいです。 – Michael