gpt4 book ai didi

java - JSON 读取对象值

转载 作者:行者123 更新时间:2023-12-02 09:37:21 25 4
gpt4 key购买 nike

我有一个来自 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"));
}
}
}

通过上面的代码,我得到的是“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)"

关于java - JSON 读取对象值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57387104/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com