gpt4 book ai didi

jquery - 使用 JSON 和 jQuery 获取带有链接、回复或主题标签的推文时跳过它们

转载 作者:行者123 更新时间:2023-12-01 06:02:11 25 4
gpt4 key购买 nike

我正在开发一个网站,用户可以在其中输入随机单词并获取相关推文列表。

在使用 json 获取包含链接、回复或主题标签的推文时,如何排除它们?

这是我的 jQuery 代码:

        <script>

function go(){
var url = "http://search.twitter.com/search.json?callback=results&q=" + $("#text").val();
$("<script/>").attr("src", url).appendTo("body");
$("#text").remove();
}

$("#text").keydown(function(e){ if( e.which == 13 ) go(); });

function results(r){
window.results = r.results;
window.theIndex = 0;
displayNext();
}
function displayNext(){
if( window.theIndex >= window.results.length ){
return;
}
$('.content').remove();
$('.helper').remove();
createDiv( window.results[window.theIndex] );
window.theIndex++;
setTimeout(displayNext, 4000);
}

function createDiv(status){
var tweets = status.text;
$("<span class='content'>")
.html(tweets)
.appendTo("body");
$("<span class='helper'>")
.appendTo("body")
}

</script>

最佳答案

根据Dev Twitter API reference ,返回的 JSON 对象包含一个 result 属性,它是表示所有推文的 JSON 对象数组。这些 JSON 数组中特别令人感兴趣的两个属性是 entitites 属性和 to_user_id 属性。因此,要检查推文是否不是回复且不包含任何链接,您需要检查 entities 是否为空对象且 to_user_id 是否为 null。

将您的 displayNext 函数更改为此应该可以:

function displayNext(){
if( window.theIndex >= window.results.length ){
return;
}
$('.content').remove();
$('.helper').remove();
var result = window.results[window.theIndex];
if (Object.keys(result.entities).length !== 0 && result.to_user_id === null) {
createDiv( window.results[window.theIndex] );
window.theIndex++;
setTimeout(displayNext, 4000);
}
}​

(请注意,我使用 How do I test for an empty JavaScript object? 中的答案来检查 entitites 是否为空对象。)

关于jquery - 使用 JSON 和 jQuery 获取带有链接、回复或主题标签的推文时跳过它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10337758/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com