Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 检查JSON中是否存在子数组_Javascript_Arrays_Json - Fatal编程技术网

Javascript 检查JSON中是否存在子数组

Javascript 检查JSON中是否存在子数组,javascript,arrays,json,Javascript,Arrays,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\u003djso

以下是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[]
是否存在。正如您所看到的,图像数组没有出现在第二个响应中,因此在我尝试将
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具有图像[],一切正常

编辑: 以下是解决方案:

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)) {
  ...
}

有没有理由不将其解析为PHP数组,然后进行检查?您为您的帖子添加了PHP标签,所以我假设您正在使用它

$myarray = json_decode($json);
if (isset($myarray->items[0]->product->images)) {
  ...
}
尝试访问不存在的对象的属性将返回undefined。这很可能就是你所犯的错误

您知道web浏览器中的web inspector工具吗?它们都有一个控制台,任何错误都可以输出到该控制台。如果你检查一下,你就会知道出了什么问题

您仍在尝试访问该条件语句的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';
}
“不等于未定义”相当于说“已定义”

尝试访问不存在的对象的属性将返回undefined。这很可能就是你所犯的错误

您知道web浏览器中的web inspector工具吗?它们都有一个控制台,任何错误都可以输出到该控制台。如果你检查一下,你就会知道出了什么问题

您仍在尝试访问该条件语句的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';
}

“not equal to undefined”相当于说“is defined”

不清楚您是在JavaScript还是在PHP中检查这个问题。您看过这个吗@罗斯,这是用javasscript写的。我删除了php标签表示歉意。不清楚你是用JavaScript还是php检查这个。你看过这个吗@罗斯,这是用javasscript写的。我删除了php标记表示歉意。我已经尝试过了,但当我尝试访问“data.items[0].product.images”时,代码仍然会中断并停止。如果它不存在,那么它甚至不会说未定义。这是当前块“如果(data.items[i].product.images==undefined){var image='images/inverticon.png';}或者{var image=data.items[i].product.images[0].thumbnails[0].link;}用正在发生的事情的确切细节更新您的答案,例如错误。另外,请澄清问题出在JS上,而不是PHP上,以便其他人观看。我需要的是您最后的建议。我想我从来没有那样做过。我想是时候休息一下了。非常感谢。:)我尝试过这个方法,但当我尝试访问“data.items[0].product.images”时,代码仍然会中断并停止。如果它不存在,那么它甚至不会说未定义。这是当前块“如果(data.items[i].product.images==undefined){var image='images/inverticon.png';}或者{var image=data.items[i].product.images[0].thumbnails[0].link;}用正在发生的事情的确切细节更新您的答案,例如错误。另外,请澄清问题出在JS上,而不是PHP上,以便其他人观看。我需要的是您最后的建议。我想我从来没有那样做过。我想是时候休息一下了。非常感谢。:)很抱歉,我使用php将我的信息返回到javascript,然后从那里返回所有js。”父母想要('','');'很抱歉,我使用php将我的信息返回到javascript,然后从那里返回所有js。”父母想要('','');'