简体   繁体   English

如何使用JavaScript删除动态创建的json对象中的多余引号?

[英]How to remove redundant quotation marks in dynamically created json object with javascript?

Following problem: I want to create an Audio player with a playlist like on this site: http://www.davidorlowsky.com/player/demos/lark.htm 出现问题:我想创建一个音频播放器,其播放列表类似于此网站: http : //www.davidorlowsky.com/player/demos/lark.htm

If you click the right mouse button and view the source code you'll see that the playlist has the following form: 如果单击鼠标右键并查看源代码,则会看到播放列表具有以下形式:

[
        {
            title:"Gypsy Hora",
            artist:"Trad.",
            mp3:"http:///www.klezmorim-online.de/lark/gypsyhora.mp3"
        },
        {
            title:"Sammy's Freilach",
            artist:"Trad.",
            mp3:"http:///www.klezmorim-online.de/lark/sammysfreilach.mp3"
        },
        ...
]

What I'm trying to do is: creating this kind of playlist dynamically via parsing through a directory. 我想做的是:通过目录解析来动态创建这种播放列表。 This is how I try to do it: 这是我尝试做到的方式:

$(document).ready(function () {                
    var sound_files = Array();
    $.ajax({
        url: "my_directory/",
        success: function (data) {
            $(data).find("td > a").each(function () {
                var file = $(this).attr("href");
                if (file.substr((file.lastIndexOf('.') + 1)) == "mp3") {
                    var mp3 = file;
                    var title= mp3.substr(0, mp3.lastIndexOf('.'));
                    //sound_files.push("{title: \""+title+"\", mp3: \"my_directory/" + mp3 + "\"}");
                    sound_files.push("{mp3: \"my_directory/" + mp3 + "\"}");
                }
            });
        }
    }).done(function () {
        console.log(sound_files);
        new jPlayerPlaylist({
            jPlayer: "#jquery_jplayer_1",
            cssSelectorAncestor: "#jp_container_1"
        }, sound_files, {
            swfPath: "jplayer",
            supplied: "oga,mp3"
        });
    });
});

It doesn't work that way and I think it's because there are redundant quotation marks in the json object. 这种方式行不通,我认为是因为json对象中有多余的引号。 The resulting object looks like this: 生成的对象如下所示:

["{mp3: "my_directory/my_file.mp3"}", ...]

but should look like: 但应如下所示:

[{mp3: "my_directory/my_file.mp3"}, ...]

How could I do this? 我该怎么办?

Do not push the object as string, rather do it as an object. 不要将对象作为字符串推送,而应将其作为对象推送。 Change this part where you are sending it: 更改发送零件的位置:

sound_files.push("{mp3: \"my_directory/" + mp3 + "\"}");

As an object: 作为对象:

sound_files.push({mp3: "my_directory/" + mp3});

尝试sound_files.push({mp3: "my_directory/" + mp3});

You are currently creating strings that contain the JavaScript source code needed to construct an object. 当前正在创建包含构造对象所需的JavaScript源代码的字符串。

Just create objects instead. 只需创建对象即可。

sound_files.push({mp3: "my_directory/" + mp3});

要获得双引号,请添加此+'“' ,一切都会起作用。但是对象方法比创建字符串要清晰得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM