0

I know this has been asked many times before, I have read through dozens of questions and answers but for some reason I can't get my code to work so please don't close this question. Here is the JSON object that my http get request returns:

{
  "tracks" : {
    "href" : "https://api.spotify.com/v1/search?query=Closer&offset=0&limit=20&type=track",
    "items" : [ {
      "album" : {
        "album_type" : "single",
        "artists" : [ {
          "external_urls" : {
            "spotify" : "https://open.spotify.com/artist/69GGBxA162lTqCwzJG5jLp"
          },
          "href" : "https://api.spotify.com/v1/artists/69GGBxA162lTqCwzJG5jLp",
          "id" : "69GGBxA162lTqCwzJG5jLp",
          "name" : "The Chainsmokers",
          "type" : "artist",
          "uri" : "spotify:artist:69GGBxA162lTqCwzJG5jLp"
        } ],
        "available_markets" : [ "AD", "AR", "AT", "AU", "BE", "BG", "BO", "BR", "CA", "CH", "CL", "CO", "CR", "CY", "CZ", "DE", "DK", "DO", "EC", "EE", "ES", "FI", "FR", "GB", "GR", "GT", "HK", "HN", "HU", "ID", "IE", "IS", "IT", "JP", "LI", "LT", "LU", "LV", "MC", "MT", "MX", "MY", "NI", "NL", "NO", "NZ", "PA", "PE", "PH", "PL", "PT", "PY", "SE", "SG", "SK", "SV", "TR", "TW", "US", "UY" ],
        "external_urls" : {
          "spotify" : "https://open.spotify.com/album/0rSLgV8p5FzfnqlEk4GzxE"
        },
        "href" : "https://api.spotify.com/v1/albums/0rSLgV8p5FzfnqlEk4GzxE",
        "id" : "0rSLgV8p5FzfnqlEk4GzxE",
        "images" : [ {
          "height" : 640,
          "url" : "https://i.scdn.co/image/512bd22e2bc73f9883b8612daf4f23acaac3c776",
          "width" : 640
        }, {
          "height" : 300,
          "url" : "https://i.scdn.co/image/2e244b9284d317b2019bb9fd44d9a086210d4974",
          "width" : 300
        }, {
          "height" : 64,
          "url" : "https://i.scdn.co/image/57ba46d39d710e99ae524b279cae3a3981ace43f",
          "width" : 64
        } ],
        "name" : "Closer",
        "type" : "album",
        "uri" : "spotify:album:0rSLgV8p5FzfnqlEk4GzxE"
      },
      "artists" : [ {
        "external_urls" : {
          "spotify" : "https://open.spotify.com/artist/69GGBxA162lTqCwzJG5jLp"
        },
        "href" : "https://api.spotify.com/v1/artists/69GGBxA162lTqCwzJG5jLp",
        "id" : "69GGBxA162lTqCwzJG5jLp",
        "name" : "The Chainsmokers",
        "type" : "artist",
        "uri" : "spotify:artist:69GGBxA162lTqCwzJG5jLp"
      }, {
        "external_urls" : {
          "spotify" : "https://open.spotify.com/artist/26VFTg2z8YR0cCuwLzESi2"
        },
        "href" : "https://api.spotify.com/v1/artists/26VFTg2z8YR0cCuwLzESi2",
        "id" : "26VFTg2z8YR0cCuwLzESi2",
        "name" : "Halsey",
        "type" : "artist",
        "uri" : "spotify:artist:26VFTg2z8YR0cCuwLzESi2"
      } ],
      "available_markets" : [ "AD", "AR", "AT", "AU", "BE", "BG", "BO", "BR", "CA", "CH", "CL", "CO", "CR", "CY", "CZ", "DE", "DK", "DO", "EC", "EE", "ES", "FI", "FR", "GB", "GR", "GT", "HK", "HN", "HU", "ID", "IE", "IS", "IT", "JP", "LI", "LT", "LU", "LV", "MC", "MT", "MX", "MY", "NI", "NL", "NO", "NZ", "PA", "PE", "PH", "PL", "PT", "PY", "SE", "SG", "SK", "SV", "TR", "TW", "US", "UY" ],
      "disc_number" : 1,
      "duration_ms" : 244960,
      "explicit" : false,
      "external_ids" : {
        "isrc" : "USQX91601347"
      },
      "external_urls" : {
        "spotify" : "https://open.spotify.com/track/7BKLCZ1jbUBVqRi2FVlTVw"
      },
      "href" : "https://api.spotify.com/v1/tracks/7BKLCZ1jbUBVqRi2FVlTVw",
      "id" : "7BKLCZ1jbUBVqRi2FVlTVw",
      "name" : "Closer",
      "popularity" : 100,
      "preview_url" : "https://p.scdn.co/mp3-preview/8d3df1c64907cb183bff5a127b1525b530992afb?cid=null",
      "track_number" : 1,
      "type" : "track",
      "uri" : "spotify:track:7BKLCZ1jbUBVqRi2FVlTVw"
    }, {

8 lines up from the bottom is the information that I want, the id: 7BKLCZ1jbUBVqRi2FVlTVw. Here is my java code I am currently using, followed by what it returns:

JSONObject jObj;
JSONObject tracks;
JSONArray items;
JSONObject id;

jObj = new JSONObject(json);    // json is the JSON string
tracks = (JSONObject) jObj.get("tracks");
items = (JSONArray) tracks.get("items");
id = items.getJSONObject(9);    // index 9 because the id is the 10th child in the structure.
Log.d("testJson", id.toString());

This is what it returns (sorry it comes out not formatted nicely):

{"album":{"album_type":"album","artists":[{"external_urls":{"spotify":"https:\/\/open.spotify.com\/artist\/6MxlVTY6PmY8Nyn16fvxtb"},"href":"https:\/\/api.spotify.com\/v1\/artists\/6MxlVTY6PmY8Nyn16fvxtb","id":"6MxlVTY6PmY8Nyn16fvxtb","name":"Slightly Stoopid","type":"artist","uri":"spotify:artist:6MxlVTY6PmY8Nyn16fvxtb"}],"available_markets":["CA","US"],"external_urls":{"spotify":"https:\/\/open.spotify.com\/album\/5FWZm9haAG0NhY6WmdS4oW"},"href":"https:\/\/api.spotify.com\/v1\/albums\/5FWZm9haAG0NhY6WmdS4oW","id":"5FWZm9haAG0NhY6WmdS4oW","images":[{"height":640,"url":"https:\/\/i.scdn.co\/image\/99614be452418aa67f429a4f16c0da7c38003481","width":640},{"height":300,"url":"https:\/\/i.scdn.co\/image\/70fa9db3e76ba2203110ae78667074ab3e9c89fa","width":300},{"height":64,"url":"https:\/\/i.scdn.co\/image\/fe326958674bb30385fa180b57bff3b8bc0d5080","width":64}],"name":"Closer to the Sun","type":"album","uri":"spotify:album:5FWZm9haAG0NhY6WmdS4oW"},"artists":[{"external_urls":{"spotify":"https:\/\/open.spotify.com\/artist\/6MxlVTY6PmY8Nyn16fvxtb"},"href":"https:\/\/api.spotify.com\/v1\/artists\/6MxlVTY6PmY8Nyn16fvxtb","id":"6MxlVTY6PmY8Nyn16fvxtb","name":"Slightly Stoopid","type":"artist","uri":"spotify:artist:6MxlVTY6PmY8Nyn16fvxtb"}],"available_markets":["CA","US"],"disc_number":1,"duration_ms":145560,"explicit":false,"external_ids":{"isrc":"US74G0560417"},"external_urls":{"spotify":"https:\/\/open.spotify.com\/track\/1UL8GkVAs6UcWF6BZ3DYZt"},"href":"https:\/\/api.spotify.com\/v1\/tracks\/1UL8GkVAs6UcWF6BZ3DYZt","id":"1UL8GkVAs6UcWF6BZ3DYZt","name":"Closer to the Sun","popularity":59,"preview_url":"https:\/\/p.scdn.co\/mp3-preview\/1d049a0243ef1286e7a237b9236801a3c8367a19?cid=null","track_number":17,"type":"track","uri":"spotify:track:1UL8GkVAs6UcWF6BZ3DYZt"}

I am wondering why it returns all of this information, but not the id for the song itself? How do I do this? Thanks

0

1 Answer 1

1

items is an array of objects... so, items.getJSONObject(9) is returning the 10-th item which is an object that hold an attribute name "id"...

In other to get the Id, you will need something like this:

JSONObject jObj;
JSONObject tracks;
JSONArray items;
String id;

jObj = new JSONObject(json);    // json is the JSON string
tracks = (JSONObject) jObj.get("tracks");
items = (JSONArray) tracks.get("items");
id = items.getJSONObject(9).getString("id");
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much! All I had to do was change getJSONObject(9) to getJSONObject(0). I messed that up before

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.