[英]Parsing JSON Array into String output in Java (Echo Nest)
I seem to be having trouble understanding how to parse nested JSON array data into strings. 我似乎在理解如何将嵌套的JSON数组数据解析为字符串方面遇到麻烦。 I'm trying to parse them through JSONArray and my data is coming from a URI so I can't use inline data (and too braindead to learn how to use GSON). 我正在尝试通过JSONArray解析它们,而我的数据来自URI,所以我不能使用内联数据(太死了,无法学习如何使用GSON)。
I'm trying to get a list of artists with just their name fields. 我正在尝试获取仅带有其姓名字段的艺术家列表。 I have only been able to output the artist array but they includes all the json values for each of their ids and names. 我只能输出artist数组,但是它们包括每个id和名称的所有json值。 All I want is the name field. 我想要的只是名称字段。 I know there is a way to get just the name fields from the artists array but I can't figure out the syntax to get it. 我知道有一种方法可以仅从Artists数组中获取名称字段,但是我不知道要获取它的语法。
Thanks in advance. 提前致谢。
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);
{"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"}]}}
Ok i will give it a try. 好吧,我会尝试一下。 Not at my developing computer, so cannot test it: 不在我正在开发的计算机上,因此无法对其进行测试:
Starting point: 初始点:
{"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");
Results in: 结果是:
{"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");
Should give: 应该给:
[{"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"}]
You are getting close, at this point I would iterate the array as I assume you cannot know beforehand how many results you get: 您正在接近,这时我将迭代数组,因为我假设您事先不知道会得到多少结果:
for (int i = 0; i < artistArray.length(); i++) {
JSONObject artist = artistArray.getJSONObject(i);
}
For index 0 this is: 对于索引0,这是:
{"id": "AR0PK561187B9B9EF9", "name": "TV on the Radio"}
Now the only thing missing is fetching the name: 现在唯一缺少的是获取名称:
for (int i = 0; i < artistArray.length(); i++) {
JSONObject artist = artistArray.getJSONObject(i);
String name = artist.getString("name");
// generate button
}
To sum up the code: 总结一下代码:
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
}
I think it would make sense to dynamically generate the buttons, again because I assume you do not know how many results you get. 我认为动态生成按钮是有道理的,因为我假设您不知道会得到多少结果。 Otherwise simply store the names in a separate list and use that list of names to show the output. 否则,只需将名称存储在单独的列表中,然后使用该名称列表显示输出。
JSONObject resultObject = new JSONObject(result);
JSONObject responseObject = resultObject.getJSONObject("response");
JSONArray artistArray = responseObject.getJSONArray("artists");
for (int i = 0; i < artistArray.length(); i++) {
JSONObject artist1 = artistArray.getJSONObject(0);
JSONObject artist2 = artistArray.getJSONObject(1);
JSONObject artist3 = artistArray.getJSONObject(2);
JSONObject artist4 = artistArray.getJSONObject(3);
JSONObject artist5 = artistArray.getJSONObject(4);
String name1 = artist1.getString("name");
String name2 = artist2.getString("name");
String name3 = artist3.getString("name");
String name4 = artist4.getString("name");
String name5 = artist5.getString("name");
Button button1 = (Button)getView().findViewById(R.id.button1);
Button button2 = (Button)getView().findViewById(R.id.button2);
Button button3 = (Button)getView().findViewById(R.id.button3);
Button button4 = (Button)getView().findViewById(R.id.button4);
Button button5 = (Button)getView().findViewById(R.id.button5);
button1.setText(name1);
button2.setText(name2);
button3.setText(name3);
button4.setText(name4);
button5.setText(name5);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.