0

我正在使用声音云 javascript api 创建一个应用程序。我在做什么从

http://api.soundcloud.com/resolve.json?url=http://soundcloud.com/matas/hobnotropic&client_id=YOUR_CLIENT_ID

这工作得很好,也返回了所需的东西,但我想如何向返回的 json 输出添加一个新变量。

目前输出如下所示

{"kind":"track","id":49931,"created_at":"2008/10/27 09:56:47 +0000","user_id":1433,"duration":489138,"commentable":true,"state":"finished","original_content_size":130032044,"sharing":"public","tag_list":"dub minimalist hypnotic flanger","permalink":"hobnotropic","streamable":true,"embeddable_by":"all","downloadable":false,"purchase_url":"https://soundcloud.com/matas","label_id":null,"purchase_title":"Get more tracks!","genre":"dub","title":"Hobnotropic","description":"Kinda of an experiment in search for my own sound. I've produced this track from 2 loops I've made using Hobnox Audiotool ( http://www.hobnox.com/audiotool.1046.en.html ). Imported into Ableton LIve! and tweaked some FX afterwards.","label_name":"","release":"","track_type":"original","key_signature":"","isrc":"","video_url":null,"bpm":84.0,"release_year":null,"release_month":null,"release_day":null,"original_format":"wav","license":"cc-by-nc","uri":"http://api.soundcloud.com/tracks/49931","user":{"id":1433,"kind":"user","permalink":"matas","username":"matas","uri":"http://api.soundcloud.com/users/1433","permalink_url":"http://soundcloud.com/matas","avatar_url":"http://i1.sndcdn.com/avatars-000001548772-zay6dy-large.jpg?16b9957"},"permalink_url":"http://soundcloud.com/matas/hobnotropic","artwork_url":"http://i1.sndcdn.com/artworks-000000103093-941e7e-large.jpg?16b9957","waveform_url":"http://w1.sndcdn.com/IqSLUxN7arjs_m.png","stream_url":"http://api.soundcloud.com/tracks/49931/stream","playback_count":74836,"download_count":280,"favoritings_count":25,"comment_count":23,"attachments_uri":"http://api.soundcloud.com/tracks/49931/attachments"}

我想要的只是添加一个变量"custom_url":"http://example.com/example123"

所以任何人都可以指导我有没有可能的方法来做到这一点。我想知道我是否可以将变量传递给soundcloud,soundcloud会自动将变量附加到响应中。

4

3 回答 3

0

首先,将 JSON 响应存储在一个变量中:

var obj = response;

因此,您现在有一个包含 javascript 对象的变量。要将属性添加到您的对象,您可以简单地使用:

obj.custom_url = "Your_url";

或者

obj["custom_url"] = "your_url";
于 2013-08-23T12:32:31.317 回答
0

我不知道完整的对象结构,但可以说对象被调用result并且有一个名为 的属性albums,它是一个对象数组。在这种情况下,你会这样做:

$.each(result.albums, function() {
    this.custom_url = 'whatever';
});

或者使用香草js:

for (var i = 0, i < result.albums.length; i++) {
    result.albums[i].custom_url = 'whatever';
}
于 2013-08-23T12:37:33.127 回答
-1

你不需要也不想要 jQuery,只需要 JavaScript。

举个例子

var data = {items: [
    {id: "1", name: "Snatch", type: "crime"},
    {id: "2", name: "Witches of Eastwick", type: "comedy"},
    {id: "3", name: "X-Men", type: "action"},
    {id: "4", name: "Ordinary People", type: "drama"},
    {id: "5", name: "Billy Elliot", type: "drama"},
    {id: "6", name: "Toy Story", type: "children"}
]};

添加项目:

data.items.push(
    {id: "7", name: "Douglas Adams", type: "comedy"}
);

这增加了结尾。在中间添加见下文。

移除一个项目:

有几种方法。拼接方法是最通用的:

data.items.splice(1, 3); // Removes three items starting with the 2nd,
                         // ("Witches of Eastwick", "X-Men", "Ordinary People")

splice 修改原始数组,并返回您删除的项目的数组。

中间添加:

splice 实际上既添加又删除。拼接方法的签名是:

removed_items = arrayObject.splice(index, num_to_remove[, add1[, add2[, ...]]]);

index - the index at which to start making changes
num_to_remove - starting with that index, remove this many entries
addN - ...and then insert these elements

所以我可以像这样在第三个位置添加一个项目:

data.items.splice(2, 0,
    {id: "7", name: "Douglas Adams", type: "comedy"}
);

这就是说:从索引 2 开始,删除零个项目,然后插入以下项目。结果如下所示:

var data = {items: [
    {id: "1", name: "Snatch", type: "crime"},
    {id: "2", name: "Witches of Eastwick", type: "comedy"},
    {id: "7", name: "Douglas Adams", type: "comedy"},     // <== The new item
    {id: "3", name: "X-Men", type: "action"},
    {id: "4", name: "Ordinary People", type: "drama"},
    {id: "5", name: "Billy Elliot", type: "drama"},
    {id: "6", name: "Toy Story", type: "children"}
]};

您可以删除一些并立即添加一些:

data.items.splice(1, 3,
    {id: "7", name: "Douglas Adams", type: "comedy"},
    {id: "8", name: "Dick Francis", type: "mystery"}
);

...这意味着:从索引 1 开始,删除三个条目,然后添加这两个条目。结果是:

var data = {items: [
    {id: "1", name: "Snatch", type: "crime"},
    {id: "7", name: "Douglas Adams", type: "comedy"},
    {id: "8", name: "Dick Francis", type: "mystery"}
    {id: "5", name: "Billy Elliot", type: "drama"},
    {id: "6", name: "Toy Story", type: "children"}
]};
于 2013-08-23T12:33:36.047 回答