JSON 读取对象值

JSON Reading Object Values

我收到来自 Spotify 的 JSON 回复,我正在尝试阅读。在我的 java 代码中,我得到了相册对象的 'name' 值,而不是跟踪对象的值。这是回复:https://pastebin.com/fcvTzJJv

在 JSON 响应的底部,有一个名称字段显示 "Money in the Grave",这就是我想从 JSON 中获取的内容,而不是 [=22] =]在专辑名称下。

下面是读取此响应的代码:

 try
            {
                JSONObject obj = new JSONObject(result);
                JSONObject tracks = obj.getJSONObject("tracks");
                JSONArray items = tracks.getJSONArray("items");

                for(int i = 0; i < items.length(); i++)
                {
                    //album object
                    JSONObject album = items.getJSONObject(i).getJSONObject("album");
                    //SHOULD be getting the images array but does not

                    //artist array
                    JSONArray artists = album.getJSONArray("artists");


                    //gets the necessary artist information
                    for(int j = 0; j < artists.length(); j++)
                    {
                        JSONObject artist = artists.getJSONObject(j);
                        songLists.add(artist.getString("name") +  " - " +album.getString("name"));
                    }
                }
            }

使用上面的代码,我得到 "The Best in the World Pack",而不是 "Money in the Grave"。有人可以帮助解释如何获取曲目名称,而不是专辑名称。谢谢

 for(int i = 0; i < items.length(); i++)
            {

                 JSONObject values  = items.getJSONObject(i);
                   String name= values.getString("name");
                    System.out.println(name+"          nameee");
                //artist array
                JSONArray artists = album.getJSONArray("artists");
  }

this will work for you and will return "Money In The Grave (Drake ft. Rick Ross)"

你可以这样试,它会给出曲目名称

String response = yourJSonData;

try {
            JSONObject jsonObject =  new JSONObject(response);
            Log.e("data","data---"+response);
           JSONArray jsArrItems = jsonObject.getJSONObject("tracks").getJSONArray("items");
            Log.e("data","data---222>>"+jsArrItems.getJSONObject(0).getString("name")+"<<>>"+jsArrItems);
        } catch (JSONException e) {
            e.printStackTrace();
        }

如果 itemsJSONArray 多于 1 那么也可以使用循环

在您的项目循环中获取 tems.getJSONObject(i).getString("name") 之类的名称,因为您正在查找的名称位于项目数组对象中,而不是艺术家对象中。