具体解析Json输出[重复]

Posted

技术标签:

【中文标题】具体解析Json输出[重复]【英文标题】:Being specific Parsing Json output [duplicate] 【发布时间】:2017-07-05 19:44:20 【问题描述】:

我有一个输出,我想从tracks 然后playlists 获取信息

来自:"total_tracks":12,"tracks":["total_playlists":6,"playlists":[

我试过了:

var obj = jQuery.parseJSON( data );
console.log(obj.tracks.uri); //Should return the uri but i receive undefined
console.log(obj.playlists.uri); //Should return the uri but i receive undefined

我要解析的 json 输出:

"query":"new release 2017","uri":"spotify:search:new+release+2017","total_tracks":12,"tracks":["artist":"Suresh Parik","title":"Aa Dado Sa Ri Laadli","album":"Rajasthani New Release 2017","duration":382000,"uri":"spotify:track:43tKsH7K2FUOSd4mnutx3m","available":true,"popularity":0,"index":1,"artist":"Shyam Singh","title":"Bhajan Me Java Koni De","album":"Rajasthani New Release 2017","duration":338000,"uri":"spotify:track:4AdNsTGLKfyAehLXrXmSUK","available":true,"popularity":0,"index":2,"artist":"Hamira Ram Raika","title":"Bhanwar Sa Mhasu Milta Jaijo","album":"Rajasthani New Release 2017","duration":370000,"uri":"spotify:track:7hEeZk8Fvb8AFd2ps553lq","available":true,"popularity":0,"index":3,"artist":"Hamira Ram Raika","title":"Dhola Dhola Dhotiya","album":"Rajasthani New Release 2017","duration":416000,"uri":"spotify:track:5YnLliPcOm4Qb1ay2SiTPm","available":true,"popularity":0,"index":4,"artist":"Papu Artiya","title":"Doda Mat Pi Dokriya","album":"Rajasthani New Release 2017","duration":359000,"uri":"spotify:track:3EjwwyV5qUPffTRUdeOQGV","available":true,"popularity":0,"index":5,"artist":"Kuldeep Ojha","title":"Gora Gora Gaal","album":"Rajasthani New Release 2017","duration":528000,"uri":"spotify:track:4WlHzN1FSXygCpuPqGLGvY","available":true,"popularity":0,"index":6,"artist":"Hamira Ram Raika","title":"Jorji Champawat","album":"Rajasthani New Release 2017","duration":557000,"uri":"spotify:track:0ixmKnWl9E2adeEvbHqD6k","available":true,"popularity":0,"index":7,"artist":"Natwar Jhitra","title":"Maa Ka Palda Bhari Hai","album":"Rajasthani New Release 2017","duration":654000,"uri":"spotify:track:33t5kA8V5dmzdcyWPfy4th","available":true,"popularity":0,"index":8,"artist":"Ramesh Lohiya","title":"Mele Khele Jao Bhanwar Ji","album":"Rajasthani New Release 2017","duration":304000,"uri":"spotify:track:0JodbnP1tyX6oorRmWOkxC","available":true,"popularity":0,"index":9,"artist":"Nena Ram Dewasi, Chhotu Banna","title":"Nach Nach Mhara Sang Ra Sathi","album":"Rajasthani New Release 2017","duration":285000,"uri":"spotify:track:5FQKtFPwOYUHaNDAtN9sWn","available":true,"popularity":0,"index":10,"artist":"Kamlesh Singh","title":"Odh Lugadi Pilo Byayan","album":"Rajasthani New Release 2017","duration":404000,"uri":"spotify:track:2***imxfZOmb33kChjcm2c","available":true,"popularity":0,"index":11,"artist":"Adrian Zenith","title":"Release Yourself - Digital Energy Remix","album":"Happy New Year from Trance of Ibiza 2017","duration":497000,"uri":"spotify:track:4krqyIAazX3R1TVbBxyTB5","available":true,"popularity":0,"index":12],"total_albums":1,"albums":["artist":"Various Artists","title":"Rajasthani New Release 2017","available":true,"uri":"spotify:album:0u1e7VZzl59NWxvIdWyquT"],"total_artists":0,"artists":[],"total_playlists":6,"playlists":["name":"Winter 2017 Country playlist","uri":"spotify:user:1228804577:playlist:0Yzxp6mxnuBfrtVzlxG03h","name":"Top New Songs 2017","uri":"spotify:user:22geympzgpcckueuh7ry6rvqy:playlist:7CPOFZtGed5SE0XhMHmiep","name":"2017 New Release","uri":"spotify:user:teamwork1:playlist:1iJy5jsCvHpp1jDWLtDhIE","name":"Jetsetter 2017 (EDM Elixir) | Updated Weekly","uri":"spotify:user:126414896:playlist:0JUtq7lLoahjuXZQARW13U","name":"2017 New Release & Hit Parade","uri":"spotify:user:12150104486:playlist:6n75nX7NBRCn2cXsmT4eHf","name":"UNFD 2017","uri":"spotify:user:unfdcentral:playlist:2Lbl7gTrFYREhHEGUV4ZNZ"]

【问题讨论】:

obj.tracks 是一个数组。您是否有关于您希望 uri 用于哪个元素的标准? 【参考方案1】:

好吧,tracksplaylists 都是数组,而数组没有 uri 组件。您需要访问数组中的一个项目:

var obj = jQuery.parseJSON( data );
console.log(obj.tracks[0].uri);
console.log(obj.playlists[0].uri);

【讨论】:

试图在 uri 上索引它。忘记轨道是一个数组。整天忙于这个工作,轻描淡写地累了。感谢您的帮助!

以上是关于具体解析Json输出[重复]的主要内容,如果未能解决你的问题,请参考以下文章

统一解析json文件[重复]

无法解析 Reddit Json [重复]

解析大型 JSON 文件 [重复]

在 iOS 中解析 JSON [重复]

JSON解析不等于原始[重复]

解析特定的 JSON 结构 [重复]