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

发布于 2024-12-22 16:27:43 字数 5027 浏览 2 评论 0原文

我正在玩一些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 或类似内容的项目进行某种默认后备?

I'm playing around with some YQL, and getting the returned result returned as JSON. Therefore I'm trying to run though it using some JavaScript. Everything works fine, but when it comes to one single element I'd like to grab, my JavaScript code breaks.

This is a piece of my 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.

I'm using the following piece of JavaScript code to grab the content that I want.

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;
}

As you can probably see, I'm running through all of the li's and I'm trying to print out each li's image, link, title and price. Almost everything works, but when I'm trying to grab the price of each product, my JavaScript breaks. I've found out that one or two of the li's that I'm iterating through, hasn't got the span.span.content, instead they've got a p.span.content. This means that in some cases the code can't find the span.span.content, and then it breaks.

Why does this happen? Is there a solution to what I can do to make this not break? Is it possible to have some kind of default fallback to the items that haven't got the span.span.content or something like that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

極樂鬼 2024-12-29 16:27:43

无法说出为什么它会像这样下降,但这会解决这种情况:

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;
}

Can't speak to why it's coming down like this, but this would fix just that scenario:

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

Or slightly more compact:

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

Or if there could be other kinds of spans you want to avoid and need to be more thorough:

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);

Or you could break it out of ternary if that's too much to look at:

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;
}

Finally, if you're okay with price being undefined, you could shorten the above slightly with this:

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文