最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Json - checking child node null value - Stack Overflow

programmeradmin0浏览0评论

How can i check if a child attribute of a json object is null? While firefox recognizes it, Chrome and IE 8 don't.

I have a json object like this:

centralData.phone.id;
centralData.address.id;
centralData.product.id;
//and many others

And i'd like to check if some of it's attributes might be null. I'm doing this and it works:

if(centralData.phone != null){
   //Do things
}

But this don't, since it's not always i have a StockGroup

if(centralData.product.StockGroup != null){
 //Error
}

So, how can i check if centralData.product.StockGroup is null?

How can i check if a child attribute of a json object is null? While firefox recognizes it, Chrome and IE 8 don't.

I have a json object like this:

centralData.phone.id;
centralData.address.id;
centralData.product.id;
//and many others

And i'd like to check if some of it's attributes might be null. I'm doing this and it works:

if(centralData.phone != null){
   //Do things
}

But this don't, since it's not always i have a StockGroup

if(centralData.product.StockGroup != null){
 //Error
}

So, how can i check if centralData.product.StockGroup is null?

Share Improve this question asked Apr 18, 2012 at 15:10 AdrianoRRAdrianoRR 1,1311 gold badge17 silver badges36 bronze badges 7
  • What are you trying to check exactly? – Sergio Tulentsev Commented Apr 18, 2012 at 15:12
  • Nitpicking: You don't have a "JSON object". You have an object or you have JSON. The latter is a data format and nothing but a STRING. – Mörre Commented Apr 18, 2012 at 15:13
  • I just edited to make more clear. Thanks for pointing that. – AdrianoRR Commented Apr 18, 2012 at 15:13
  • @Mörre I didn't know it wasn't called json object. Guess i mistook it because of java's class JSONObject. – AdrianoRR Commented Apr 18, 2012 at 15:15
  • For your 2nd example, if centralData.product does not exist, OF COURSE you get an error. You have to check like this: "if(centralData.product && centralData.product.StockGroup != null)" It IS a little inconvenient, but you have to check ALL ELEMENTS of the path individually and not just the leaf element of the tree structure. Not to mention that you should use !== (or ===) and be clear if you want to check null or undefined. – Mörre Commented Apr 18, 2012 at 15:15
 |  Show 2 more ments

4 Answers 4

Reset to default 4

I suppose you would check if a property is undefined instead

if(typeof (centralData.product || {}).StockGroup) !== "undefined") {
   /* do something */
}

this kind of check was described on ajaxian site and the overall code is much shorter

You don't want to check if it's null, you want to check if the property exists (it would be undefined in this case). Your check works because you are using == instead of ===, which converts between types (undefined == null, but undefined !== null).

When you want to check for a nested property, you need to check every level. I would remend to use the in operator, as it checks if the property exists and ignores it's value.

This does what you want to do:

if("product" in centralData && "StockGroup" in centralData.product){
    …
}

Instead of using null, try paring your object with undefined.

Since JavaScript employs lazy evaluation, you can perform checks like this with no detriment to performance:

if(centralData.product != null && centralData.product.StockGroup != null){

}
发布评论

评论列表(0)

  1. 暂无评论