[英]Parsing JSON Array into String output in Java (Echo Nest)
我似乎在理解如何將嵌套的JSON數組數據解析為字符串方面遇到麻煩。 我正在嘗試通過JSONArray解析它們,而我的數據來自URI,所以我不能使用內聯數據(太死了,無法學習如何使用GSON)。
我正在嘗試獲取僅帶有其姓名字段的藝術家列表。 我只能輸出artist數組,但是它們包括每個id和名稱的所有json值。 我想要的只是名稱字段。 我知道有一種方法可以僅從Artists數組中獲取名稱字段,但是我不知道要獲取它的語法。
提前致謝。
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"}]}}
好吧,我會嘗試一下。 不在我正在開發的計算機上,因此無法對其進行測試:
初始點:
{"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
}
我認為動態生成按鈕是有道理的,因為我假設您不知道會得到多少結果。 否則,只需將名稱存儲在單獨的列表中,然后使用該名稱列表顯示輸出。
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.