2011-12-22 66 views
0

我在玩一些YQL,并将返回的结果作为JSON返回。因此,我试图通过使用一些JavaScript来运行它。一切工作正常,但当涉及到一个单一的元素,我想抢,我的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或类似的东西?

回答

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

或者你可以打破它的t ernary如果这是太多看:

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

完美杰德!非常感谢。 +1并且正确答案。 – 2011-12-22 21:46:51