簡體   English   中英

檢查是否在JSON中存在子數組

[英]check to see if a sub array exists in JSON

這是json樣本:

    {
   "kind": "shopping#product",
   "id": "tag:google.com,2010:shopping/products/6582229/17914968800165668776",
   "selfLink": "https://www.googleapis.com/shopping/search/v1/public/products/6582229/gid/17914968800165668776?alt\u003djson",
   "product": {
    "googleId": "17914968800165668776",
    "author": {
     "name": "Red Tag Market",
     "accountId": "6582229"
    },
    "creationTime": "2010-11-30T10:00:00.000Z",
    "modificationTime": "2011-05-01T09:20:00.000Z",
    "country": "US",
    "language": "en",
    "title": "The Fantastic Mr. Fox - BLU-RAY/DVD",
    "description": "Wit.",
    "link": "ht361",
    "condition": "new",
    "gtin": "00024543657552",
    "inventories": [
     {
      "channel": "online",
      "price": 22.51,
      "currency": "USD"
     }
    ],
    "images": [
     {
      "link": "http://208.131.143.232/i/6/3/0/7/6/1/8.jpg",
      "thumbnails": [
       {
        "width": 60,
        "height": 60,
        "link": "hBEevU46OsArJElwIeErF_3E7Zzu12M2eLSvQBdYiMLaRWrI60aF8lHxRqOz-wkx2YJUIVdCrzrEQDWxgcc"
       }
      ]
     }
    ]
   }
  },
  {
   "kind": "shopping#product",
   "id": "tag:google.com,2010:shopping/products/6296724/17894083551590155418",
   "selfLink": "https://www.googleapis.com/shopping/search/v1/public/products/6296724/gid/17894083551590155418?alt\u003djson",
   "product": {
    "googleId": "17894083551590155418",
    "author": {
     "name": "eBay",
     "accountId": "6296724"
    },
    "creationTime": "2011-04-04T00:43:02.000Z",
    "modificationTime": "2011-04-04T00:43:02.000Z",
    "country": "US",
    "language": "en",
    "title": "Fy.",
    "link": "htt530831212&itemid\u003d140530831212&icep_meta_categ_id\u003d11232",
    "condition": "used",
    "gtin": "00024543657552",
    "inventories": [
     {
      "channel": "online",
      "price": 14.99,
      "currency": "USD"
     }
    ],
    "images": [
     {
      "link": "http://i.ebayimg.com/00/%24%28KGrHqYOKkQE1r4Vh1gFBNl4n0t17g%7E%7E_1.JPG?set_id\u003d8800005007",
      "thumbnails": [
       {
        "width": 60,
        "height": 60,
        "link": "http://lh
       }
      ]
     }
    ]
   }
  }

我想做的是查看data.items[i].product.images[]存在。 如您所見,Images數組未出現在第二個響應中,因此在嘗試出於任何目的使用data.items[i].product.images[]那一刻殺死了我的JavaScript。 我一直在尋找並且尚未找到解決方案。

編輯:我只是嘗試此,仍然沒有骰子:

if(data.items[i].product.images !== undefined){
    var image = 'images/inverticon.png';
}else{
    var image = data.items[i].product.images[0].thumbnails[0].link;
}

我沒有收到任何錯誤,腳本只是停止運行。 如果我省略了圖像代碼,則一切正常,只要返回的JSON具有image [],它就可以正常工作。

編輯:這是解決方案:

if(data.items[i].product.images !== undefined){
    var image = data.items[i].product.images[0].thumbnails[0].link;
}else{
    var image = 'images/inverticon.png';
}
if (data.items[i].product.images !== undefined) {
  //do what you will
}

嘗試訪問不存在的對象的屬性將返回undefined。 這很可能是您遇到的錯誤?

您了解Web瀏覽器中的Web檢查器工具嗎? 它們都有一個控制台,所有錯誤都將輸出到該控制台。 如果您進行檢查,就會發現出了什么問題。

您仍在嘗試在該條件語句的else中訪問images數組,這將導致錯誤。

應該是這樣

if(data.items[i].product.images !== undefined){
  var image = data.items[i].product.images[0].thumbnails[0].link;

} else {
  var image = 'images/inverticon.png';
}

“不等於未定義”等同於說“已定義”

您是否有理由不將其解析為PHP數組,然后進行檢查? 您標記了您的帖子PHP,所以我認為這就是您所使用的。

$myarray = json_decode($json);
if (isset($myarray->items[0]->product->images)) {
  ...
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM