javascript检查json是否为null [英] javascript check if null from json
本文介绍了javascript检查json是否为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用以下JavaScript.它写得很好,直到得到没有值的结果为止.在控制台日志中,它显示了
Im using the following javascript. it writes fine until it gets to a result which doesn't have a value. in console log it shows this
未捕获的TypeError:无法读取null的属性文本"
Uncaught TypeError: Cannot read property 'text' of null
但是我下面的脚本似乎不起作用
but my script below doesn't seem to work
var checkCaption = photo.caption.text;
if (checkCaption == null) {
caption = 'meh';
} else {
caption = photo.caption.text;
}
推荐答案
在您的示例中,photo.caption
为空,因此您的代码在完成检查之前在photo.caption.text
调用上中断.
In your example, photo.caption
is null, so your code breaks on the photo.caption.text
call, before the check is done.
var caption;
if(photo.caption != null) { // Covers 'undefined' as well
caption = photo.caption.text;
} else {
caption = "meh";
}
这篇关于javascript检查json是否为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文