0

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"}]}}

2 Answers2

1

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.

cYrixmorten
  • 7,110
  • 3
  • 25
  • 33
  • That looks like a way to get ahead, but I don't know a good way to generate buttons. I'm a bit new with Java and having a difficult time getting some of these concepts down. Do I have to generate them with another loops or something like: String nameString1 = name.toString();? – Jesse Spalding Dec 16 '14 at 23:43
  • You can see the answer to this question for inspiration regarding buttons: http://stackoverflow.com/questions/7195056/how-do-i-programmatically-add-buttons-into-layout-one-by-one-in-several-lines simply do button.setText(name), to make the button have the name of the artist. – cYrixmorten Dec 17 '14 at 00:39
0

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