我有一个string
变量 songs
,其中包含来自流媒体日志文件的以下内容:
[{'url':'https://open.spotify.com/track/0TLAptKgYxe7F0KewWH6X6','title':'I Need A Forest Fire','album':'The Colour In Anything','artist':'James Blake, Bon Iver','coverUrl':'https://i.scdn.co/image/7b8f132a8a91d46cce43b42bd0916f16708c8358','date':'May 21, 2016 at 04:41PM'},
{'url':'https://open.spotify.com/track/3cBV8V9zlYNraydyF8NpBY','title':'Dress','album':'Sylvan Esso','artist':'Sylvan Esso','coverUrl':'https://i.scdn.co/image/d64189ee29314326a188f1a334dfd20c085dfb7e','date':'May 22, 2016 at 06:53PM'},
{'url':'https://open.spotify.com/track/6W3CjUdc26sI1Y7cmelM5A','title':'Stutter','album':'Good Future','artist':'EMEFE','coverUrl':'https://i.scdn.co/image/10669383f3ca6f90b6692e2da9b6601f020078ff','date':'May 23, 2016 at 12:57AM'},
{'url':'https://open.spotify.com/track/1GxNPd5r7D1zChEMuMhue0','title':'Only','album':'Dawn','artist':'RY X','coverUrl':'https://i.scdn.co/image/281bc0be895562e7146b361931330ec5a586d1ba','date':'May 24, 2016 at 01:55AM'}]
如何在JavaScript中将其转换为实际数组?我试过JSON.parse()
,但失败了。是因为值是单引号而不是双引号? (我无法控制流数据文件。)
答案 0 :(得分:3)
您可以使用 eval()
var songs = `[{'url':'https://open.spotify.com/track/0TLAptKgYxe7F0KewWH6X6','title':'I Need A Forest Fire','album':'The Colour In Anything','artist':'James Blake, Bon Iver','coverUrl':'https://i.scdn.co/image/7b8f132a8a91d46cce43b42bd0916f16708c8358','date':'May 21, 2016 at 04:41PM'},
{'url':'https://open.spotify.com/track/3cBV8V9zlYNraydyF8NpBY','title':'Dress','album':'Sylvan Esso','artist':'Sylvan Esso','coverUrl':'https://i.scdn.co/image/d64189ee29314326a188f1a334dfd20c085dfb7e','date':'May 22, 2016 at 06:53PM'},
{'url':'https://open.spotify.com/track/6W3CjUdc26sI1Y7cmelM5A','title':'Stutter','album':'Good Future','artist':'EMEFE','coverUrl':'https://i.scdn.co/image/10669383f3ca6f90b6692e2da9b6601f020078ff','date':'May 23, 2016 at 12:57AM'},
{'url':'https://open.spotify.com/track/1GxNPd5r7D1zChEMuMhue0','title':'Only','album':'Dawn','artist':'RY X','coverUrl':'https://i.scdn.co/image/281bc0be895562e7146b361931330ec5a586d1ba','date':'May 24, 2016 at 01:55AM'}]`;
var res = eval(songs);
console.log(res);
请参阅以下问题:Why is using the JavaScript eval function a bad idea?
或者将所有单引号替换为double,然后使用 JSON.parse()
var songs = `[{'url':'https://open.spotify.com/track/0TLAptKgYxe7F0KewWH6X6','title':'I Need A Forest Fire','album':'The Colour In Anything','artist':'James Blake, Bon Iver','coverUrl':'https://i.scdn.co/image/7b8f132a8a91d46cce43b42bd0916f16708c8358','date':'May 21, 2016 at 04:41PM'},
{'url':'https://open.spotify.com/track/3cBV8V9zlYNraydyF8NpBY','title':'Dress','album':'Sylvan Esso','artist':'Sylvan Esso','coverUrl':'https://i.scdn.co/image/d64189ee29314326a188f1a334dfd20c085dfb7e','date':'May 22, 2016 at 06:53PM'},
{'url':'https://open.spotify.com/track/6W3CjUdc26sI1Y7cmelM5A','title':'Stutter','album':'Good Future','artist':'EMEFE','coverUrl':'https://i.scdn.co/image/10669383f3ca6f90b6692e2da9b6601f020078ff','date':'May 23, 2016 at 12:57AM'},
{'url':'https://open.spotify.com/track/1GxNPd5r7D1zChEMuMhue0','title':'Only','album':'Dawn','artist':'RY X','coverUrl':'https://i.scdn.co/image/281bc0be895562e7146b361931330ec5a586d1ba','date':'May 24, 2016 at 01:55AM'}]`;
var res = JSON.parse(songs.replace(/'/g, '"'));
console.log(res);
答案 1 :(得分:0)
javascript eval
将完成这项工作。
var str = "[{'url':'https://open.spotify.com/track/0TLAptKgYxe7F0KewWH6X6','title':'I Need A Forest Fire','album':'The Colour In Anything','artist':'James Blake, Bon Iver','coverUrl':'https://i.scdn.co/image/7b8f132a8a91d46cce43b42bd0916f16708c8358','date':'May 21, 2016 at 04:41PM'},{'url':'https://open.spotify.com/track/3cBV8V9zlYNraydyF8NpBY','title':'Dress','album':'Sylvan Esso','artist':'Sylvan Esso','coverUrl':'https://i.scdn.co/image/d64189ee29314326a188f1a334dfd20c085dfb7e','date':'May 22, 2016 at 06:53PM'},{'url':'https://open.spotify.com/track/6W3CjUdc26sI1Y7cmelM5A','title':'Stutter','album':'Good Future','artist':'EMEFE','coverUrl':'https://i.scdn.co/image/10669383f3ca6f90b6692e2da9b6601f020078ff','date':'May 23, 2016 at 12:57AM'},{'url':'https://open.spotify.com/track/1GxNPd5r7D1zChEMuMhue0','title':'Only','album':'Dawn','artist':'RY X','coverUrl':'https://i.scdn.co/image/281bc0be895562e7146b361931330ec5a586d1ba','date':'May 24, 2016 at 01:55AM'}]"
eval(str)
另一种方法
var f = new Function('return ' + str );
var arr = f();
您可以查看此SO thread,了解eval
和new Function
答案 2 :(得分:0)
将所有单引号替换为double并使用JSON.parse(),如下所示:
var str = "[{'url':'https://open.spotify.com/track/0TLAptKgYxe7F0KewWH6X6','title':'I Need A Forest \ Fire','album':'The Colour In Anything','artist':'James Blake, Bon\ Iver','coverUrl':'https://i.scdn.co/image/7b8f132a8a91d46cce43b42bd0916f16708c8358','date':'May 21, 2016 at 04:41PM'}, \
{'url':'https://open.spotify.com/track/3cBV8V9zlYNraydyF8NpBY','title':'Dress','album':'Sylvan Esso','artist':'Sylvan Esso','coverUrl':'https://i.scdn.co/image/d64189ee29314326a188f1a334dfd20c085dfb7e','date':'May 22, 2016 at 06:53PM'}, \
{'url':'https://open.spotify.com/track/6W3CjUdc26sI1Y7cmelM5A','title':'Stutter','album':'Good Future','artist':'EMEFE','coverUrl':'https://i.scdn.co/image/10669383f3ca6f90b6692e2da9b6601f020078ff','date':'May 23, 2016 at 12:57AM'}, \
{'url':'https://open.spotify.com/track/1GxNPd5r7D1zChEMuMhue0','title':'Only','album':'Dawn','artist':'RY X','coverUrl':'https://i.scdn.co/image/281bc0be895562e7146b361931330ec5a586d1ba','date':'May 24, 2016 at 01:55AM'}]";
str = str.replace(/'/g, '"');
var strJson = JSON.parse(str);
console.log(strJson);
尝试Fiddle