gpt4 book ai didi

java - 在 Java 中将 JSON 数组解析为字符串输出(Echo Nest)

转载 作者:行者123 更新时间:2023-12-01 12:07:06 29 4
gpt4 key购买 nike

我似乎无法理解如何将嵌套的 JSON 数组数据解析为字符串。我正在尝试通过 JSONArray 解析它们,并且我的数据来自 URI,因此我无法使用内联数据(而且学习如何使用 GSON 也太脑残了)。

我正在尝试获取仅包含姓名字段的艺术家列表。我只能输出艺术家数组,但它们包含每个 id 和名称的所有 json 值。我想要的只是名称字段。我知道有一种方法可以从艺术家数组中获取名称字段,但我无法弄清楚获取它的语法。

提前致谢。

这是我到目前为止所拥有的:

JSONObject resultObject = new JSONObject(result);
JSONObject responseObject = resultObject.getJSONObject("response");
JSONArray artistArray = responseObject.getJSONArray("artists : name");
String nameString1 = artistArray.getString(0);
String nameString2 = artistArray.getString(1);
String nameString3 = artistArray.getString(2);
String nameString4 = artistArray.getString(3);
String nameString5 = artistArray.getString(4);
Button output1 = (Button)getView().findViewById(R.id.button1);
Button output2 = (Button)getView().findViewById(R.id.button2);
Button output3 = (Button)getView().findViewById(R.id.button3);
Button output4 = (Button)getView().findViewById(R.id.button4);
Button output5 = (Button)getView().findViewById(R.id.button5);
output1.setText(nameString1);
output2.setText(nameString2);
output3.setText(nameString3);
output4.setText(nameString4);
output5.setText(nameString5);

来自 Echo Nest URL 的我的 JSON 数据

{"response": {"status": {"version": "4.2", "code": 0, "message": "Success"}, "artists": [{"id": "AR0PK561187B9B9EF9", "name": "TV on the Radio"}, {"id": "ARH6W4X1187B99274F", "name": "Radiohead"}, {"id": "ARAKQSI1257509D1DC", "name": "Rave Radio"}, {"id": "ARYCW5M1187B98DB6A", "name": "Radical Face"}, {"id": "ARVJWUX14801150165", "name": "Radio Doria"}]}}

最佳答案

好的,我会尝试一下。不在我的开发计算机上,因此无法测试它:

起点:

{"response": {"status": {"version": "4.2", "code": 0, "message": "Success"}, "artists": [{"id": "AR0PK561187B9B9EF9", "name": "TV on the Radio"}, {"id": "ARH6W4X1187B99274F", "name": "Radiohead"}, {"id": "ARAKQSI1257509D1DC", "name": "Rave Radio"}, {"id": "ARYCW5M1187B98DB6A", "name": "Radical Face"}, {"id": "ARVJWUX14801150165", "name": "Radio Doria"}]}}

JSONObject resultObject = new JSONObject(result);
JSONObject responseObject = resultObject.getJSONObject("response");

结果:

{"status": {"version": "4.2", "code": 0, "message": "Success"}, "artists": [{"id": "AR0PK561187B9B9EF9", "name": "TV on the Radio"}, {"id": "ARH6W4X1187B99274F", "name": "Radiohead"}, {"id": "ARAKQSI1257509D1DC", "name": "Rave Radio"}, {"id": "ARYCW5M1187B98DB6A", "name": "Radical Face"}, {"id": "ARVJWUX14801150165", "name": "Radio Doria"}]}

JSONArray artistArray = responseObject.getJSONArray("artists");

应该给出:

[{"id": "AR0PK561187B9B9EF9", "name": "TV on the Radio"}, {"id": "ARH6W4X1187B99274F", "name": "Radiohead"}, {"id": "ARAKQSI1257509D1DC", "name": "Rave Radio"}, {"id": "ARYCW5M1187B98DB6A", "name": "Radical Face"}, {"id": "ARVJWUX14801150165", "name": "Radio Doria"}]

你已经很接近了,此时我将迭代数组,因为我假设你无法事先知道你得到了多少结果:

for (int i = 0; i < artistArray.length(); i++) {
JSONObject artist = artistArray.getJSONObject(i);
}

对于索引 0,这是:

{"id": "AR0PK561187B9B9EF9", "name": "TV on the Radio"}

现在唯一缺少的是获取名称:

for (int i = 0; i < artistArray.length(); i++) {
JSONObject artist = artistArray.getJSONObject(i);
String name = artist.getString("name");
// generate button
}

总结一下代码:

JSONObject resultObject = new JSONObject(result);
JSONObject responseObject = resultObject.getJSONObject("response");
JSONArray artistArray = responseObject.getJSONArray("artists");
for (int i = 0; i < artistArray.length(); i++) {
JSONObject artist = artistArray.getJSONObject(i);
String name = artist.getString("name");
// generate button
}

我认为动态生成按钮是有意义的,因为我假设您不知道会得到多少结果。否则,只需将名称存储在单独的列表中,并使用该名称列表来显示输出。

关于java - 在 Java 中将 JSON 数组解析为字符串输出(Echo Nest),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27515414/

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