gpt4 book ai didi

javascript - 检查json中是否存在某个元素

转载 作者:可可西里 更新时间:2023-11-01 02:28:47 25 4
gpt4 key购买 nike

使用以下提要:

http://api.twitter.com/1/statuses/user_timeline.json?screen_name=microsoft&include_rts=1&count=10

我能够成功地循环遍历它以获取我想要在我的 html 页面上显示的详细信息。

但是,我需要检查 retweeted_status.user.profile_image_url 是否存在,但我不确定该怎么做?

目前我正在遍历 jquery-ajax 返回的 data 数据:

data[i].retweeted_status.user.profile_image_url

如果 data[i].retweeted_status.user.profile_image_url 不存在,它不会返回 null 或 undefined,我只会得到以下错误:

无法读取未定义的属性“user”

最佳答案

该错误消息表明您的 data[i] 或您的 retweeted_status 未定义,这意味着您获取 json 的调用很可能失败。当数据从 Twitter 返回时,它将失去个人资料图片 url,因为它们为那些没有上传的人显示类似鸡蛋的图片。你应该把它放在你的 for 循环之前:

if (typeof(retweeted_status) != "undefined")
{
//code for what to do with json here
}

关于javascript - 检查json中是否存在某个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6548150/

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