我需要检查一个元素是否在json数据中。
JSON数据
{
"status": {
"carts": [
{
"service": "page-mailer",
"running": "",
"last success": "2015-05-07 09:35:31"
}
],
"pages": [
{
"service": "page-mailer",
"running": "",
"last success": "2015-05-07 09:35:31"
}
],
"actions": [
{
"service": "page-mailer",
"running": "",
"last success": "2015-05-07 09:35:31"
}
],
"integrations": [],
"service": [
{
"service": "page-mailer",
"running": "",
"last success": "2015-05-07 09:35:31"
}
],
"smtp": [
{
"Server": "myserver"
}
],
"servers": []
},
"timing": "2ms"
}
类似的东西,
if(carts){}
if(pages){} etc
我正在使用jquery getJSON();
答案 0 :(得分:2)
下面应该适合你
if(data && data.status && data.status.carts){
console.log("Exist")
}
答案 1 :(得分:1)
如果你有:
var data = {"status": { "carts": ....... } }
然后检查是否存在carts
:
if( data && data.status && data.status.carts ) {
//do something
}