Java 使用多个级别解析JSON对象
我知道这已经被问了很多次了,我已经阅读了几十个问题和答案,但由于某些原因,我无法让我的代码工作,所以请不要关闭这个问题。以下是http get请求返回的JSON对象:Java 使用多个级别解析JSON对象,java,android,json,Java,Android,Json,我知道这已经被问了很多次了,我已经阅读了几十个问题和答案,但由于某些原因,我无法让我的代码工作,所以请不要关闭这个问题。以下是http get请求返回的JSON对象: { "tracks" : { "href" : "https://api.spotify.com/v1/search?query=Closer&offset=0&limit=20&type=track", "items" : [ { "album" : { "
{
"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:7BKLCZ1JBUBVQRI2FFLTVW
。下面是我当前使用的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());
这是它返回的内容(很抱歉,它的格式不好):
我想知道为什么它返回所有这些信息,而不是歌曲本身的id?我该怎么做?谢谢
项目
是一个对象数组。。。因此,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");
非常感谢你!我所要做的就是将getJSONObject(9)更改为getJSONObject(0)。我以前把它搞砸了
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");