向声音云响应json添加一个变量

Adding a variable to sound cloud response json.

本文关键字:一个 变量 添加 声音 响应 json      更新时间:2023-09-26

我正在使用声音云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会自动将变量附加到响应中。

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

var obj = response;

现在,您有了一个变量,其中包含一个javascript对象。要将属性添加到您的对象,只需使用:

obj.custom_url = "Your_url";

obj["custom_url"] = "your_url";

我不知道完整的对象结构,但假设对象被称为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';
}

u不需要或想要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")

拼接将修改原始阵列,并返回已删除项目的阵列。

中间添加:

拼接实际上既可以添加也可以删除。拼接方法的特征是:

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