Java 如何从这个JSON对象获取艺术家名称的值?

Java 如何从这个JSON对象获取艺术家名称的值?,java,json,Java,Json,我有下面的JSON对象(抱歉长度),我需要它来获取艺术家的名字。它应该在曲目->项目->专辑->艺术家->名称下。我正在尝试运行此代码,但无法获取艺术家名称。以下是JSON对象的一部分: { "tracks" : { "href" : "https://api.spotify.com/v1/search?query=closer&type=track&offset=0&limit=20", "items" : [ { "album" : {

我有下面的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" ],

我需要在上面写着“名字”的那一行中找到“<代码>”,,但我想不出来。这就是我到目前为止所做的:

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
jsonobjectjobj;
JSONObject轨迹;
JSONArray项目;
JSONObject相册;
杰索纳雷艺术家;
JSONObject aName;
jObj=新的JSONObject(json);
tracks=(JSONObject)jObj.get(“tracks”);
items=(JSONArray)跟踪.get(“items”);
字符串词名;
Log.d(“searchSong”,json);
//返回搜索歌曲的所有结果。这将返回到具有歌曲名称、uri和艺术家名称的列表视图。
对于(int i=0;i

这是一个for循环,因为我需要获取所有歌曲,这里我只发布了部分歌曲的JSON对象。如果您发现任何问题,请告诉我,谢谢

什么是艺术家。getJSONObject(4)
?您的数组似乎只显示数组中的一个对象

应该只是

artists.getJSONObject(0).getString("name")

什么是艺术家。getJSONObject(4)?您的数组似乎只显示数组中的一个对象

应该只是

artists.getJSONObject(0).getString("name")

它会抛出错误吗?你得到的是什么?似乎是一个标准的调试问题。设置断点将对您有所帮助。另外,
String artistsName=artists.getJSONObject(4.toString(),您可能希望尝试使用索引3。只是一种预感。你找不到Spotify的Java API吗?我已经处理了很长时间了,它还没有完成,甚至Spotify也没有完成,所以很难为Medos工作。它会出错吗?你得到的是什么?似乎是一个标准的调试问题。设置断点将对您有所帮助。另外,
String artistsName=artists.getJSONObject(4.toString(),您可能希望尝试使用索引3。只是一种预感。你找不到Spotify的Java API吗?我已经处理了很长一段时间了,它还没有完成,甚至Spotify也没有完成,所以很难为方法工作getJSONObject(4)是因为“name”是artists JSONArray中的第四个对象。它实际上应该是getJSONObject(3),忘记了它的索引为0。@Michael“因为“name”是Artisters JSONArray中的第四个对象,”事实上,不是。它是数组第一个对象中的第四个键。需要更仔细地检查支架。JSON键是无序的,因此按索引获取一个键是不可行的。getJSONObject(4)是因为“name”是JSONArray中的第四个对象。它实际上应该是getJSONObject(3),忘记了它的索引为0。@Michael“因为“name”是Artisters JSONArray中的第四个对象,”事实上,不是。它是数组第一个对象中的第四个键。需要更仔细地检查支架。JSON键是无序的,所以按索引获取一个键不起作用。