2013-08-23 40 views
0

我正在使用声音云api创建应用程序。我在做什么,从将变量添加到声音云响应json。

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

得到JSON这工作得很好,并返回所需的东西,但我想怎么过到一个新的变量添加到返回的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会自动将该变量追加到响应中。

+1

所以,你有一个JavaScript对象,让我们把它叫做'result',和你想的属性添加到它?你尝试过'result.custom_url ='http:// example.com/example123'吗? –

+0

是的,我已经尝试过,它只能工作,但如果它的播放列表,然后有一个问题。由于单个结果内有多个结果,因此我需要为每个结果执行此操作。你明白我的意思吗 ? –

+0

看看['jQuery.each()'](http://api.jquery.com/jQuery.each/) – NDM

回答

0

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

var obj = response; 

所以,你现在持有JavaScript对象的变量。将属性添加到您的对象,你简单的使用:

obj.custom_url = "Your_url"; 

obj["custom_url"] = "your_url"; 
-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") 

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

添加在中间:

拼接实际上做添加并删除。拼接方法的签名是:

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

我认为你的答案与问题无关。 –

+0

我在给一个主意亲爱的@NirmalRam –

+0

但我已经从http://stackoverflow.com/questions/4538269/adding-removing-items-from-json-data-with-jquery获得主意 您刚刚复制粘贴。 –

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'; 
}