向声音云响应json添加变量。

时间:2013-08-23 12:24:50

标签: javascript jquery json soundcloud

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

获取json
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会自动将变量附加到响应中。

3 个答案:

答案 0 :(得分:0)

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

var obj = response;

所以,你现在有一个包含javascript对象的变量。要向对象添加属性,只需使用:

obj.custom_url = "Your_url";

obj["custom_url"] = "your_url";

答案 1 :(得分:0)

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

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

或者使用vanilla js:

for (var i = 0, i < result.albums.length; i++) {
    result.albums[i].custom_url = 'whatever';
}

答案 2 :(得分:-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

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

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"}
]};