解析具有多个Android级别的JSON对象

编程入门 行业动态 更新时间:2024-10-14 16:19:46
本文介绍了解析具有多个Android级别的JSON对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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" : "api.spotify/v1/search?query=Closer&offset=0&limit=20&type=track", "items" : [ { "album" : { "album_type" : "single", "artists" : [ { "external_urls" : { "spotify" : "open.spotify/artist/69GGBxA162lTqCwzJG5jLp" }, "href" : "api.spotify/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" : "open.spotify/album/0rSLgV8p5FzfnqlEk4GzxE" }, "href" : "api.spotify/v1/albums/0rSLgV8p5FzfnqlEk4GzxE", "id" : "0rSLgV8p5FzfnqlEk4GzxE", "images" : [ { "height" : 640, "url" : "i.scdn.co/image/512bd22e2bc73f9883b8612daf4f23acaac3c776", "width" : 640 }, { "height" : 300, "url" : "i.scdn.co/image/2e244b9284d317b2019bb9fd44d9a086210d4974", "width" : 300 }, { "height" : 64, "url" : "i.scdn.co/image/57ba46d39d710e99ae524b279cae3a3981ace43f", "width" : 64 } ], "name" : "Closer", "type" : "album", "uri" : "spotify:album:0rSLgV8p5FzfnqlEk4GzxE" }, "artists" : [ { "external_urls" : { "spotify" : "open.spotify/artist/69GGBxA162lTqCwzJG5jLp" }, "href" : "api.spotify/v1/artists/69GGBxA162lTqCwzJG5jLp", "id" : "69GGBxA162lTqCwzJG5jLp", "name" : "The Chainsmokers", "type" : "artist", "uri" : "spotify:artist:69GGBxA162lTqCwzJG5jLp" }, { "external_urls" : { "spotify" : "open.spotify/artist/26VFTg2z8YR0cCuwLzESi2" }, "href" : "api.spotify/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" : "open.spotify/track/7BKLCZ1jbUBVqRi2FVlTVw" }, "href" : "api.spotify/v1/tracks/7BKLCZ1jbUBVqRi2FVlTVw", "id" : "7BKLCZ1jbUBVqRi2FVlTVw", "name" : "Closer", "popularity" : 100, "preview_url" : "p.scdn.co/mp3-preview/8d3df1c64907cb183bff5a127b1525b530992afb?cid=null", "track_number" : 1, "type" : "track", "uri" : "spotify:track:7BKLCZ1jbUBVqRi2FVlTVw" }, {

从底部开始的8行是我想要的信息,id:7BKLCZ1jbUBVqRi2FVlTVw.这是我当前正在使用的Java代码,后面是返回的内容:

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\/artist\/6MxlVTY6PmY8Nyn16fvxtb"},"href":"https:\/\/api.spotify\/v1\/artists\/6MxlVTY6PmY8Nyn16fvxtb","id":"6MxlVTY6PmY8Nyn16fvxtb","name":"Slightly Stoopid","type":"artist","uri":"spotify:artist:6MxlVTY6PmY8Nyn16fvxtb"}],"available_markets":["CA","US"],"external_urls":{"spotify":"https:\/\/open.spotify\/album\/5FWZm9haAG0NhY6WmdS4oW"},"href":"https:\/\/api.spotify\/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\/artist\/6MxlVTY6PmY8Nyn16fvxtb"},"href":"https:\/\/api.spotify\/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\/track\/1UL8GkVAs6UcWF6BZ3DYZt"},"href":"https:\/\/api.spotify\/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?我该怎么做呢?谢谢

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

推荐答案

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

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"...

要获得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");

更多推荐

解析具有多个Android级别的JSON对象

本文发布于:2023-11-30 09:00:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1649445.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   级别   对象   Android   JSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!