Dan*_*hen 3 javascript api ajax jquery json
首先,我通过Web服务器获取JSON数据
$.getJSON(url,function(){
//my callback function;
});
Run Code Online (Sandbox Code Playgroud)
现在我有以下数据:
{entries:[{title:'foo',id:'UUID',finished:null},{title:'bar',id:'UUID',finished:null},{title:'baz',id:'UUID',finished:null}]}
Run Code Online (Sandbox Code Playgroud)
我必须通过它的UUID找到一个特定的JSON条目,然后我需要修改一个部分,例如,创建一个新的json数据:
{title:'foo',id:'UUID',finished:true}
Run Code Online (Sandbox Code Playgroud)
并使用发送回服务器
$.post(url, data);
Run Code Online (Sandbox Code Playgroud)
我完全迷失了这种情况......任何人都可以帮忙吗?
假设您已将数据放入一个名为的变量中result,如下所示:
var result = {entries:[{title:'foo',id:'UUID',finished:null},{title:'bar',id:'UUID',finished:null},{title:'baz',id:'UUID',finished:null}]}
Run Code Online (Sandbox Code Playgroud)
你可以做一个for循环:
for ( var i=0; i<result.entries.length; i++ ) {
if (result.entries[i].id == 'the_UUID_you_are_looking_for') {
var entry = result.entries[i]; // "entry" is now the entry you were looking for
// ... do something useful with "entry" here...
}
}
Run Code Online (Sandbox Code Playgroud)
编辑 - 我已经在下面写了完整的解决方案,以进一步说明这个想法并避免误解:
// Get data from the server
$.getJSON("url", function(result) {
// Loop through the data returned by the server to find the UUId of interest
for ( var i=0; i<result.entries.length; i++ ) {
if (result.entries[i].id == 'the_UUID_you_are_looking_for') {
var entry = result.entries[i];
// Modifiy the entry as you wish here.
// The question only mentioned setting "finished" to true, so that's
// what I'm doing, but you can change it in any way you want to.
entry.finished = true;
// Post the modified data back to the server and break the loop
$.post("url", result);
break;
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11023 次 |
| 最近记录: |