检查 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';
}
Here is the json Sample:
{
"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
}
]
}
]
}
}
What I want to do is see if data.items[i].product.images[]
exists or not. as you can see the Images array does not present in the second response thus killing my javascript at the point I try to use data.items[i].product.images[]
for any purpose. I have been looking and have yet to find a solution.
Edit:
I just tried this and still no dice:
if(data.items[i].product.images !== undefined){
var image = 'images/inverticon.png';
}else{
var image = data.items[i].product.images[0].thumbnails[0].link;
}
I do not get any errors, the script just stops running. If I omit the image code everything works fine and as long as the returned JSON has the image[] it works fine.
Edit:
here is the solution:
if(data.items[i].product.images !== undefined){
var image = data.items[i].product.images[0].thumbnails[0].link;
}else{
var image = 'images/inverticon.png';
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试访问不存在的对象的属性将返回未定义。这很可能是您遇到的错误?
您知道网络浏览器中的网络检查器工具吗?它们都有一个控制台,任何错误都会输出到该控制台。如果你检查一下,你就会发现出了什么问题。
您仍在尝试访问该条件语句的 else 中的图像数组,这将导致错误。
应该是这样的,
'不等于未定义'相当于说'已定义'
trying to access a property of an object that does not exist will return undefined. That's most likely the error you were getting?
Do you know about the web inspector tool in web browsers? They all have a console that any errors get outputted to. If you check that, then you'll see what's wrong.
You're still trying to access the images array in the else of that condition statement, that will cause an error.
It should be like this,
'not equal to undefined' is equivalent to saying 'is defined'
您是否有理由不将其解析为 PHP 数组然后检查?您标记了您的帖子 PHP,所以我假设这就是您正在使用的。
Is there a reason you don't parse it into a PHP array then check? You tagged your post PHP so I assume that's what you're using.