[英]convert string array object into array object - Javascript
I have a flask route where I am trying to pipe a list_object [{}]
into a template.我有一个 flask 路线,我试图将 pipe 一个 list_object
[{}]
放入模板中。 It works but the data comes out as a string on the front end.它可以工作,但数据在前端以字符串形式出现。 How can I convert the string back into a array_object so I can do stuff with it?
如何将字符串转换回 array_object 以便我可以用它做一些事情?
Here is the flask route: app.py这是 flask 路线:app.py
@app.route('/vgmplayer')
def vgmplayer():
musicdata = music.query.order_by(func.random()).first()
musicimgdata = musicimages.query.filter_by(gamealbum = musicdata.gamealbum).order_by(func.random()).first()
if musicimgdata == None:
musicimgdata = "https://media.idownloadblog.com/wp-content/uploads/2018/03/Apple-Music-icon-003.jpg"
else:
musicimgdata = musicimgdata.gameart
musicobject = [{
"name": musicdata.gametrackname,
"path": musicdata.gamelink,
"img": musicimgdata,
"album": musicdata.gamealbum,
"artists": musicdata.artists,
"platform": musicdata.platform,
"year": musicdata.year,
"genre": musicdata.genre
}]
print(musicobject)
return render_template("musicplayer.html",rvgm = musicobject)
but when I get back the template: musicplayer.html但是当我取回模板时:musicplayer.html
<script type="text/javascript">
function grvgm(){
All_song = "{{ rvgm|safe }}"
return All_song
}
</script>
it comes in a string:它有一个字符串:
Example data:
All_song = grvgm()
"[{'name': '10 - M Stage Jungle B', 'path': 'https://vgmsite.com/soundtracks/zhuzhu-pets-quest-for-zhu-20…-nds-gamerip/ebulpznnyw/10%20-%20M%20Stage%20Jungle%20B.mp3', 'img': None, 'album': 'ZhuZhu Pets - Quest for Zhu', 'artists': None, 'platform': 'DS', 'year': '2011', 'genre': None}]"
I would need the list_dict to not have the qoutes at the end so that the javascript can treat it as an array.我需要 list_dict 最后没有 qoutes,以便 javascript 可以将其视为数组。
EDIT: I forgot to mention that I tried:编辑:我忘了提到我试过:
function grvgm(){
All_song = "{{ rvgm }}"
All_song = JSON.parse(All_song)
return All_song
}
SyntaxError: JSON.parse: expected property name or '}' at line 1 column 3 of the JSON data
Turns out the string was not a valid json format as @Andy pointed out.事实证明,正如@Andy 指出的那样,该字符串不是有效的 json 格式。 I had to use a different method.
我不得不使用不同的方法。 Instead of piping the list_dict into the template, I used a get request to get the list_dict into the front end so I can do stuff with it.
我没有使用管道将 list_dict 导入模板,而是使用 get 请求将 list_dict 导入前端,这样我就可以用它做一些事情了。
app.py应用程序.py
@app.route('/grvgm', methods=['GET'])
def grvgm():
musicdata = music.query.order_by(func.random()).first()
musicimgdata = musicimages.query.filter_by(gamealbum = musicdata.gamealbum).order_by(func.random()).first()
if musicimgdata == None:
musicimgdata = "https://media.idownloadblog.com/wp-content/uploads/2018/03/Apple-Music-icon-003.jpg"
else:
musicimgdata = musicimgdata.gameart
musicobject = [{
"name": musicdata.gametrackname,
"path": musicdata.gamelink,
"img": musicimgdata,
"album": musicdata.gamealbum,
"artists": musicdata.artists,
"platform": musicdata.platform,
"year": musicdata.year,
"genre": musicdata.genre
}]
print(musicobject)
musicobject = json.dumps(musicobject)
return musicobject
musicplayer.js音乐播放器.js
async function load_track(index_no){
fetch('/grvgm')
.then(response => response.json())
.then(All_song => {
console.log(All_song)
// stuff goes here
})
You can use JSON.parse(string)
this will parse the string into a javascript array您可以使用
JSON.parse(string)
这会将字符串解析为 javascript 数组
also question was already answered here: Convert string array to array in javascript这里也已经回答了问题: Convert string array to array in javascript
By using the JSON.parse() method, you can easily parse the string.通过使用 JSON.parse() 方法,您可以轻松解析字符串。
Example:例子:
JSON.parse(All_song)
// returns an array
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.