Javascript 检查 json 中是否存在元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6548150/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 22:14:37  来源:igfitidea点击:

Checking if an element exists in json

javascriptjsonjqueryjsonp

提问by oshirowanen

using the following feed:

使用以下提要:

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

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

I am successfully able to loop through this to get the details I want to display on my html page.

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

However, I need to check if retweeted_status.user.profile_image_urlexists, and I am not sure how to do that?

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

Currently I am looping through the data where datais returned by jquery-ajax:

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

data[i].retweeted_status.user.profile_image_url

data[i].retweeted_status.user.profile_image_url

if data[i].retweeted_status.user.profile_image_urldoes not exist, it does not return null or undefined, I just get the following error:

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

cannot read property 'user' of undefined

cannot read property 'user' of undefined

回答by Liam Bailey

That error message suggests that either your data[i] or your retweeted_status is undefined, which means your call to get the json is most likely failing. WHen data is returned from Twitter it will aways have a profile image url, because they show an egg-like image for those with none uploaded. You should put this before your for loop:

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

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

回答by z_inx

I think it's a good answer:

我认为这是一个很好的答案:

if('key' in myObj)

回答by Faiz Mohamed Haneef

if (something !== undefined) {
   ...
}

... or even better:

......甚至更好:

if (typeof something != "undefined") {
    ...
}

回答by Dino Gambone

If you are looping and accessing nesting objects:

如果您正在循环和访问嵌套对象:

if(!data)
    return;

while(data.length)
{
    var tweet_data = data.shift();

    /* do some work */

    if(!tweet_data|| !tweet_data.retweeted_status)
        continue;

    var retweeted_status = tweet_data.retweeted_status;

    /* do your other work */
}

This loop no longer needs to use array indexes, which isn't the most efficient unless you specifically need the index for something. Instead, it pops the first element off the array and using that object directly. The loop internals can then check the existence of a property on tweet_data.

这个循环不再需要使用数组索引,这不是最有效的,除非你特别需要索引。相反,它从数组中弹出第一个元素并直接使用该对象。然后循环内部可以检查 上的属性是否存在tweet_data

I try to avoid using more than 1 level of dereferencing for 2 reasons (like objA.child.property): 1) You can check the existence of an object from the start and if it doesn't exist, jump out or assign default values. 2) Everytime JavaScript needs to access the method property, it first has to find childwithing objAand then access property. Could be slow in large loops.

我尽量避免使用超过 1 个级别的取消引用,原因有两个(例如objA.child.property):1) 您可以从一开始就检查对象是否存在,如果不存在,则跳出或分配默认值。2) 每次 JavaScript 需要访问方法时property,它首先必须找到childwithingobjA然后访问property。在大循环中可能会很慢。

回答by Chris Marais

Take a look at $.grep(). It helps you loop through JSON objects. I used it on the link you provided. I used it to loop through the tweets and find the retweeted user images. You can have a look at my code here: http://jsfiddle.net/q5sqU/7/

看看$.grep()。它可以帮助您遍历 JSON 对象。我在你提供的链接上使用了它。我用它来遍历推文并找到转发的用户图像。您可以在这里查看我的代码:http: //jsfiddle.net/q5sqU/7/

回答by Max Stern

Another thing you can do, which has worked for me:

您可以做的另一件事对我有用:

if(retweeted_status) {
  // your code
}