开发者社区> 问答> 正文

解析花括号内的内容

我目前正在为spotify api构建一个前端。spotify api会返回这些结果。 我只想解析“id”、“name”和“artist”…我是一个新手,请帮忙我怎么做

"messageCode":200,"result":{"imageurl":"http:\/\/example.com","data":[{"id":101,"name":"","type":"Dynamic","seeall":false,"vertical":false,"list":[]},{"id":"8_121","name":"Weekly Top 20","type":"songs","seeall":true,"vertical":false,"section_id":"26","list":[{"id":"1801_1800323_7","title":"Se\u00f1orita","artist":"Shawn Mendes,Camila Cabello","subtitle":"Shawn Mendes","image":"1801\/1800323\/1800323_1564237811_200x200.jpg","type":"songs","albumid":"1800323"},{"id":"1696_1695386_1","title":"On My Way","artist":"Alan Walker,Sabrina Carpenter,Farruko","subtitle":"On My Way","image":"1696\/1695386\/1695386_1552085406_200x200.jpg","type":"songs","albumid":"1695386"},{"id":"1840_1839953_1","title":"Memories","artist":"Maroon 5","subtitle":"Memories","image":"1840\/1839953\/1839953_1569036611_200x200.jpg","type":"songs","albumid":"1839953"},{"id":"1889_1888580_1","title":"Ahimsa","artist":"U2,A.R Rahman","subtitle":"Ahimsa","image":"1889\/1888580\/1888580_1574289013_200x200.jpg","type":"songs","albumid":"1888580"},{"id":"1712_1711742_1","title":"Sugar & Brownies","artist":"Dharia","subtitle":"Sugar & Brownies","image":"1712\/1711742\/1711742_1553572206_200x200.jpg","type":"songs","albumid":"1711742"},{"id":"1787_1786076_1","title":"Beautiful People (feat. Khalid)","artist":"Ed Sheeran, Khalid","subtitle":"Beautiful People (feat. Khalid)","image":"1787\/1786076\/1786076_1562329809_200x200.jpg","type":"songs","albumid":"1786076"},{"id":"1845_1844331_1","title":"Lean On","artist":"Celina Sharma,Emiway Bantai","subtitle":"Lean On","image":"1845\/1844331\/1844331_1569490212_200x200.jpg","type":"songs","albumid":"1844331"},{"id":"1871_1870887_1","title":"Lose You To Love Me","artist":"Selena Gomez","subtitle":"Lose You To Love Me","image":"1871\/1870887\/1870887_1572291013_200x200.jpg","type":"songs","albumid":"1870887"},

问题来源StackOverflow 地址:/questions/59387135/parse-stuff-within-curly-brackets

展开
收起
kun坤 2019-12-25 21:41:54 783 0
1 条回答
写回答
取消 提交回答
  • 首先,您给的返回格式应该是一个 JSON。 所以我认为您给的这个例子不完整,并不符合JSON 格式,具体可以去 bejson.com 网站做一下格式校验。 补全之后的样子应该是:

    { "result": { "imageurl": "http://example.com", "data": [{ "id": 101, "name": "", "type": "Dynamic", "seeall": false, "vertical": false, "list": [] }, { "id": "8_121", "name": "Weekly Top 20", "type": "songs", "seeall": true, "vertical": false, "section_id": "26", "list": [{ "id": "1801_1800323_7", "title": "Se\u00f1orita", "artist": "Shawn Mendes,Camila Cabello", "subtitle": "Shawn Mendes", "image": "1801/1800323/1800323_1564237811_200x200.jpg", "type": "songs", "albumid": "1800323" }, { "id": "1696_1695386_1", "title": "On My Way", "artist": "Alan Walker,Sabrina Carpenter,Farruko", "subtitle": "On My Way", "image": "1696/1695386/1695386_1552085406_200x200.jpg", "type": "songs", "albumid": "1695386" }, { "id": "1840_1839953_1", "title": "Memories", "artist": "Maroon 5", "subtitle": "Memories", "image": "1840/1839953/1839953_1569036611_200x200.jpg", "type": "songs", "albumid": "1839953" }, { "id": "1889_1888580_1", "title": "Ahimsa", "artist": "U2,A.R Rahman", "subtitle": "Ahimsa", "image": "1889/1888580/1888580_1574289013_200x200.jpg", "type": "songs", "albumid": "1888580" }, { "id": "1712_1711742_1", "title": "Sugar & Brownies", "artist": "Dharia", "subtitle": "Sugar & Brownies", "image": "1712/1711742/1711742_1553572206_200x200.jpg", "type": "songs", "albumid": "1711742" }, { "id": "1787_1786076_1", "title": "Beautiful People (feat. Khalid)", "artist": "Ed Sheeran, Khalid", "subtitle": "Beautiful People (feat. Khalid)", "image": "1787/1786076/1786076_1562329809_200x200.jpg", "type": "songs", "albumid": "1786076" }, { "id": "1845_1844331_1", "title": "Lean On", "artist": "Celina Sharma,Emiway Bantai", "subtitle": "Lean On", "image": "1845/1844331/1844331_1569490212_200x200.jpg", "type": "songs", "albumid": "1844331" }, { "id": "1871_1870887_1", "title": "Lose You To Love Me", "artist": "Selena Gomez", "subtitle": "Lose You To Love Me", "image": "1871/1870887/1870887_1572291013_200x200.jpg", "type": "songs", "albumid": "1870887" }] }] } }

    把上面这个 JSON 字符串传递到前端就很容易了: let jret = JSON.parse('xxxx'); //xxx 就是你的返回内容 然后就很容易了: let dataArr = jret["result"]["data"]; 就拿到 data 数组了 dataArr[0]["id"] 就拿到第一个的 id 了

    2020-03-15 10:05:23
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载