-4

Using a GET request to get an artist (search) from the echonest API I get the following JSON back:

{
    "response": {
        "status": {
            "version": "4.2",
            "code": 0,
            "message": "Success"
        },
        "artists": [
            {
                "id": "ARR3ONV1187B9A2F49",
                "name": "Muse"
            }
        ]
    }
}

I want to convert the above JSON string to a JSON object like this:

jso = new JSONObject(JSONstring);

Then I want to save both the id and name into strings, first I want to save the array of artists in a JSON array like this:

jsa = jso.getJSONArray("artists");

This is the moment where I get the JSON error no value for artists.

I can't figure out what is going wrong here, can anyone help me in the right direction? Thanks.

Pragnesh Ghoda シ
  • 8,318
  • 3
  • 25
  • 40
Niek Jonkman
  • 1,014
  • 2
  • 13
  • 31

5 Answers5

1

The json array artists is inside the json object response

So you have to get the Json object with key response first, then get json array artists from it

jsa = jso.getJSONObject("response");
jsa.getJSONArray("artists");
Emil
  • 2,786
  • 20
  • 24
1

Artists array is response JSON object so first you to get response data and then after you get artists JSON array like below

JSONObject response = jso.getJSONObject("response");
jsa = response.getJSONArray("artists");

Hope it will help you !!

0

You have it nested within the response object so you are currently trying to access "artists" like it is the 'parent' object but this is in fact a child of the "response" object. You will need to first retrieve the json object to "response" and then get the json array "artists".

Vistari
  • 697
  • 9
  • 21
0

The reason you are not getting value is because you are not following nesting.

You have to receive object like its nested. First you have to goto MainObject and then data is stored in "response" object. After that you can receive array from "response" object.

You must follow nesting of JSON like it designed.

Try Doing it this way..

JSONObject mainObj = new JSONObject(JSONstring);

 // Try to get Response Object from main JSON object then try to get array from it.
JSONObject responseObj = mainObj.getJSONObject("response");

// getting array from response object.
JSONArray artistArr = responseObj.getJSONArray("artists");

Or you can do it in one line..

JSONArray artistArr = mainObj.getJSONObject("response").getJSONArray("artists");
Pragnesh Ghoda シ
  • 8,318
  • 3
  • 25
  • 40
0

"artists" array is inside the "response" object, you can get the "artists" array using following code:

jsonArray = jso.getJSONObject("response").getJSONArray("artists");
Abdul Aleem
  • 679
  • 8
  • 31