现在我正在提供一个JSON提要的内容并显示
(function () {
var twitterLikes = "data.json";
//Grab the JSON
$.getJSON(twitterLikes, {
format: "json"
})
.done(function (data) {
//iterate through each item
$.each(data.likes, function (i, likes) {
//Print out JSON results
var twitterLike = '<div class="twitterLike" id="' + likes.id + '"><img class="twitterLikeImage" src="' + likes.image + '"><p class="twitterLikeCaption">' + likes.caption + '</p><a class="twitterLikeLink" href="' + likes.link + '" target="_blank">See original post</a></div>';
//Append printed reuslts to parent container
document.getElementById("twitterLikes").innerHTML += twitterLike;
//Cap the results
if (i === 12) {
return false;
}
});
});
})();
这需要修改。我有另一个JSON文件,我已经创建了某种&#34; Locked&#34;文件,它基本上将特定帖子锁定在给定位置。
{ "locked":[{"id":90210, "position":3}] }
如何修改我的函数来解析第二个JSON文件(已锁定)。基本上查看锁定的文件,看看是否有id和位置。将该id与第一个JSON文件(data.json)匹配。将基于id的帖子锁定到特定位置。浏览data.json并绘制跳过锁定位置的其余帖子。最多只能显示12个。
答案 0 :(得分:0)
我不确定我是否理解你的锁定机制,但有一些变体(我使用了lodash来帮助 - 但它没有必要)
$.getJSON("data.json", { format: "json" })
.done(function (data) {
$.getJSON("locked.json", {format: "json"})
.done(function(locked){
var lockedIds = _.map(locked, function(l){
return l.id
})
$.each(data.likes, function(i, likes){
// If not locked then process
if (!_.contains(likes.id, lockedIds)){
// do something here
}
})
})
}
希望这有帮助。
答案 1 :(得分:0)
谢谢乔。这是我到目前为止所得到的: $ .getJSON(&#34; data.json&#34;,{format:&#34; json&#34;})
.done(function( data ) {
$.getJSON("locked.json", {format: "json"})
.done(function(locked){
var lockedIds = _.map(locked, function(l){
return l.id
})
//iterate through each item
$.each( data.likes, function( i, likes ) {
if (!_.contains(likes.id, lockedIds)){
//Print out JSON results
var twitterLike='<div class="twitterLike" id="'+likes.id+'"><img class="twitterLikeImage" src="'+likes.image+'"><p class="twitterLikeCaption">'+likes.caption+'</p><a class="twitterLikeLink" href="'+likes.link+'" target="_blank">See original post</a></div>';
//Append printed reuslts to parent container
document.getElementById("twitterLikes").innerHTML+=twitterLike;
}
//Cap the results
if ( i === 12 ) {
return false;
}
});
});
})();
我似乎有点偏僻。你是对的,我不一定需要把两者结合起来。更需要在&#34;锁定&#34;中显示这些帖子。位置,然后从原始饲料移动到其余部分。
<强> DATA.JSON 强>
{
"count": 2,
"likes": [
{
"link": "http://twitter.com/p/tQc2YNh3ox/",
"caption": "Yada",
"profile_pic": "",
"likes": 27,
"id": "815278414704900657_1364335477",
"image": "http://twitter.com/pic1.png"
}, {
"link": "http://twitter.com/p/tQc2YNh3ox2/",
"caption": "Yada2",
"profile_pic": "",
"likes": 27,
"id": "815078520979421610_1364335477",
"image": "http://twitter.com/pic2.png"
}
]
}
<强> LOCKED.JSON 强>
{
"likes": [
{
"id": "815278414704900657_1364335477",
"position": 3
}, {
"id": "815078520979421610_1364335477",
"position": 5
}
]
}