gpt4 book ai didi

javascript - 在 JavaScript 中将数组作为字符串转换为数组

转载 作者:行者123 更新时间:2023-11-28 18:34:20 25 4
gpt4 key购买 nike

我有一个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() 但失败了。是因为这些值是用单引号而不是双引号括起来的吗? (我无法控制流数据文件。)

最佳答案

您可以使用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?

<小时/>

或者将所有单引号替换为双引号,然后使用 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);

关于javascript - 在 JavaScript 中将数组作为字符串转换为数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37407067/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com