【发布时间】:2014-11-20 09:37:24
【问题描述】:
我正在使用 $.get 获取运行良好的 JSON 文件内容,目前我想过滤并获取 ID 为 4(最后一个)的项目,我应该怎么做? 在 Jquery 文档中我没有找到一些提示...... http://api.jquery.com/jquery.get/
这是代码:
$.get('tweets.json');
这是 JSON 文件内容
[
{
"id": 1,
"tweet": "OMG, worst day ever, my BF @BobbyBoo dumped me",
"usersMentioned": [
{
"id": 10,
"username": "BobbyBoo"
}
]
},
{
"id": 2,
"tweet": "OMG, best day ever, my BF came back to me"
},
{
"id": 3,
"tweet": "OMG, worst day ever, just don't ask"
},
{
"id": 4,
"tweet": "@BobbyBoo OMG...just OMG!",
"usersMentioned": [
{
"id": 10,
"username": "BobbyBoo"
}
]
}
]
更新
当前,当我尝试以下操作时,我在 then(function (tweets) 中没有得到任何东西,tweets 是 emtpy 值 entry4 填充了正确的值...
$.get('profile.json').then(function (profile) {
$('#profile-pre').html(JSON.stringify(profile));
$.get('tweets.json', null, null, 'json')
.then(function (response) {
var entry4 = response.filter(function (tweet) {
return tweet.id === 4;
})[0];
return entry4 ;
})
}).then(function (tweets) {
$('#tweets-pre').html(JSON.stringify(tweets));
var myFriend = $.get('friend.json');
return myFriend
}).then(function (friend) {
【问题讨论】:
标签: javascript jquery json promise