如果对象不存在,如何不破解JavaScript代码?

时间:2011-12-22 21:17:24

标签: javascript json undefined yql

我正在玩一些YQL,并将返回的结果作为JSON返回。因此,我试图使用一些JavaScript来运行它。一切正常,但是当我想要抓住一个单独的元素时,我的JavaScript代码会中断。

这是我的JSON:

cbfunc({
    "query": {
        "count": 30,
        "created": "2011-12-22T20:48:45Z",
        "lang": "en-US",
            "diagnostics": {
                "publiclyCallable": "true",
                "url": {
                    "execution-start-time": "1",
                    "execution-stop-time": "2214",
                    "execution-time": "2213",
                    "proxy": "DEFAULT",
                    "content": "http://www.example.com"
                },
                "user-time": "2254",
                "service-time": "2213",
                "build-version": "24402"
            },
            "results": {
                "li": [
                    {
                        "class": "item",
                        "div": [
                            {
                                "class": "onsale",
                                "p": {
                                    "class": "product-image",
                                    "a": {
                                        "href": "http://www.example.com",
                                        "title": "linktitle",
                                        "img": {
                                            "src": "http://www.example.com/image.jpg",
                                        }
                                    }
                                }
                            },
                            {
                                "class": "price-box",
                                "span": {
                                    "class": "normal-price",
                                    "span": {
                                        "class": "price",
                                        "content": "900,-"
                                    }
                                }
                            }
                        ],
                        "h5": {
                            "a": {
                                "href": "http://www.example.com",
                                "content": "Link content"
                            }
                        },
                    },
                    {
                        "class": "item",
                        "div": [
                            {
                                "class": "onsale",
                                "p": {
                                    "class": "product-image",
                                    "a": {
                                        "href": "http://www.example.com/2.html",
                                        "title": "Link title",
                                        "img": {
                                            "src": "http://www.example.com/image2.jpg",
                                        }
                                    }
                                }
                            },
                            {
                                "class": "price-box",
                                "span": {
                                    "class": "normal-price",
                                        "span": {
                                            "class": "price",
                                            "content": "812,-"
                                        }
                                    }
                                }
                            ],
                            "h5": {
                                "a": {
                                    "href": "http://www.example.com/2.html",
                                    "content": "Link 2 content"
                                }
                            },
                        }
etc.

我正在使用以下JavaScript代码来获取我想要的内容。

function cbfunc(o){
    var items = o.query.results.li;
    var output = '';
    var no_items=items.length;
    for(var i=0;i<no_items;i++){
        var img = items[i].div[0].p.a.img.src;
        var price1 = items[i].div[1];
        var price = price1.span.span.content;
        var title = items[i].h5.a.content;
        var link = items[i].h5.a.href;
        var desc = items[i].description;
        output += "<img src='" + img + "' /><h3><a href='" + link + "'>"+title+"</a></h3>" + price + "<hr/>";
    }
    // Place product in div tag
    document.getElementById('results').innerHTML = output;
}

正如您可能看到的那样,我正在浏览所有li,我正在尝试打印每个li的图像,链接,标题和价格。几乎所有东西都有效,但是当我试图抓住每个产品的价格时,我的JavaScript会中断。我发现我正在迭代的li中的一个或两个没有得到span.span.content,而是有p.span.content。这意味着在某些情况下代码无法找到span.span.content,然后就会中断。

为什么会这样?我能做些什么来解决这个问题?是否有可能对没有span.span.content或类似内容的项目进行某种默认回退?

1 个答案:

答案 0 :(得分:2)

无法说出它为什么会这样下来,但这样可以解决这个问题:

var price = price1.span ? price1.span.span.content : price1.p.span.content;

或稍微紧凑:

var price = (price1.span ? price1.span : price1.p).span.content;

或者,如果可能存在其他类型的跨度,您需要避免并且需要更彻底:

var price = price1.span && price1.span.span && price1.span.span.content ? price1.span.content : (price1.p && price1.p.span && price1.p.span.content ? price1.p.span.content : null);

或者你可以将它从三元组中分解出来,如果这太过分了,那就不用了:

var price;

if (price1.span && price1.span.span && price1.span.span.content) {
    price = price1.span.content;
} else if (price1.p && price1.p.span && price1.p.span.content) {
    price = price1.p.span.content;
} else {
    price = null;
}

最后,如果您对价格未定义感到满意,可以稍微缩短以上内容:

var price;

if (price1.span && price1.span.span) {
    price = price1.span.content;
} else if (price1.p && price1.p.span) {
    price = price1.p.span.content;
} else {
    price = null;
}