【问题标题】:JSON Reading Object ValuesJSON 读取对象值
【发布时间】:2019-08-07 04:46:15
【问题描述】:

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

在 JSON 响应的底部,有一个名称字段,上面写着“Money in the Grave”,这就是我想从 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"));
                    }
                }
            }

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

【问题讨论】:

  • 将所有 JSON 数据放入您的问题中
  • 很多,但我会包括整个回复
  • 你可以使用pastebin.com

标签: java android spotify


【解决方案1】:
 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)”

【讨论】:

    【解决方案2】:

    您可以这样尝试,它会给出该曲目的名称

    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,那么也可以使用循环

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 2013-03-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-11-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多