使用Jquery更新JSON对象中的字段值

时间:2013-12-28 10:49:40

标签: jquery json string

我有一个JSON对象:

 [{"file":"post_files/feb641027629b6a7ba78fb47063c6763vid.flv","title":"Sample"},
  {"file":"post_files/74c73cf0e28d6ca431ab4c2e2299f39fbarsandtone.flv","title":"Bars"}]

如您所见,file字段的值为post_file/feb.................flv

我想要的只是将http://divinotech.in/附加到所有file字段'值,即值应按此编辑:

http://divinotech.in/post_file/feb.................flv

我该怎么做?

2 个答案:

答案 0 :(得分:3)

您可以循环遍历数组的元素并修改file属性:

var array = [
    { "file": "post_files/feb641027629b6a7ba78fb47063c6763vid.flv", "title":"Sample"}, 
    { "file": "post_files/74c73cf0e28d6ca431ab4c2e2299f39fbarsandtone.flv", "title":"Bars"}
];

for (var i = 0; i < array.length; i++) {
    array[i].file = 'http://divinotech.in/' + array[i].file;
}

// at this stage the array will contain the desired values

这是一个live demo

答案 1 :(得分:0)

试试这个

var result=[{"file":"post_files/feb641027629b6a7ba78fb47063c6763vid.flv","title":"Sample"},
  {"file":"post_files/74c73cf0e28d6ca431ab4c2e2299f39fbarsandtone.flv","title":"Bars"}]
for (var i in result) {
$("body").append('<a href="http://divinotech.in/'+result[i].file+'">'+result[i].title+'</a></br>');
} 

DEMO