首页 > 解决方案 > JSON 读取对象值

问题描述

我有一个来自 Spotify 的 JSON 响应,我试图从中读取。在我的 java 代码中,我得到的是专辑对象的“名称”值,而不是被跟踪的对象。这是回复:https ://pastebin.com/fcvTzJJv

在 JSON 响应的底部,有一个名称字段,上面写着“坟墓里的钱”,这就是我想从 JSON 中得到的,而不是名称下的“世界上最好的包”专辑。

这是读取此响应的代码:

 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"));
                    }
                }
            }

使用上面的代码,我得到的是“世界上最好的包”,而不是“坟墓里的钱”。有人可以帮助解释如何获取曲目名称,而不是专辑名称。谢谢

标签: javaandroidspotify

解决方案


 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");
  }

这对你有用,并且会返回“Money In The Grave (Drake ft. Rick Ross)”


推荐阅读