使用多个级别Android

时间:2017-01-17 22:29:01

标签: java android json

我知道之前已经多次询问过,我已经阅读了几十个问题和答案,但由于某种原因我无法让我的代码工作,所以请不要关闭这个问题。这是我的http get请求返回的JSON对象:

{
  "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行是我想要的信息,id:7BKLCZ1jbUBVqRi2FVlTVw。这是我目前使用的java代码,后面是它返回的内容:

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

这就是它返回的内容(抱歉它没有很好地格式化):

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

我想知道为什么它会返回所有这些信息,而不是歌曲本身的id?我该怎么做呢?感谢

1 个答案:

答案 0 :(得分:1)

items是一个对象数组...因此,items.getJSONObject(9)返回第10个项目,该项目是一个包含属性名称“id”的对象...

在其他方面获取ID,你需要这样的东西:

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