java - 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"));
}
}
}
使用上面的代码,我得到的是“世界上最好的包”,而不是“坟墓里的钱”。有人可以帮助解释如何获取曲目名称,而不是专辑名称。谢谢
解决方案
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)”
推荐阅读
- java - 在 Spring Controller 中丢弃 Http 请求
- matrix - 将函数应用于矩阵的每一列
- wordpress - 剥离 Wordpress 导航中的所有默认类并替换为自定义类
- excel - 复制并粘贴到黄色突出显示的行下方
- python - 从 Python Numba CUDA 内核调用的加速 FFT
- flutter - 如何在颤动中向 CircleAvatar Widget 内部的图像添加 Onclick 事件?
- r - 网格中单位表达式的奇怪解释
- python - Pandas - 合并具有相同列名但独占值的 2 个 df
- javascript - 在 XML 中获取带有序数和条件的元素值
- ssis - 如何在更新编辑器中将映射列分配给变量?