简体   繁体   中英

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. 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).

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. 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.

Thanks in advance.

Here is what I have so far:

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);

My JSON Data from The Echo Nest URL

{"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:

{"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.

This is what I ended up coding in because I'm lazy and burnt out. Thanks @cYrixmorten

                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);
                }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM