如何检查JSON中是否存在某个键

15
我有一个JSON对象,我想检查该JSON对象中是否设置了键。
这是JSON对象:
var Data_Array = {
    "Private": {
        "Price": {
            "Adult": "18",
            "Child": [{
                "FromAge": "0",
                "ToAge": "12",
                "Price": "10"
            }]
        }
    }
}

如果 JSON 对象像这样,你可以看到它不存在Child这个属性,那么如何进行检查?

If JSON Object like this as you can see Child is not exist, then how to check this


var Data_Array = {
    "Private": {
        "Price": {
            "Adult": "18"
        }
    }
}

我已经尝试过

if(Data_Array.Private.Price.Child[0].Price != "undefined"){
    ...
}

但是它显示了这个错误

未捕获的类型错误:无法读取属性

我无法找出该怎么做。


3
Data_Array.Private.Price.hasOwnProperty("Child") 可以翻译为:数据数组私有的价格是否拥有"Child"属性。 - VLAZ
4
FYI: 你并不拥有一个“JSON对象”。这是一个普遍的用词错误。JSON代表“JavaScript对象表示法”(Javascript Object Notation)。因此,任何有效的JSON格式都是有效的JavaScript格式。你所拥有的是一个“对象”。JSON 是一种文本格式。 - Mike Cluck
“JSON对象”这种东西不存在。 - Felix Kling
@MikeC,我同意你的话,并将其牢记在心中,非常感谢你提供的信息。 - User97798
4个回答

29

var json = {key1: 'value1', key2: 'value2'}

"key1" in json ? console.log('key exists') : console.log('unknown key')

"key3" in json ? console.log('key exists') : console.log('unknown key')

针对子键

var Data_Array = {
    "Private": {
        "Price": {
            "Adult": "18",
            "Child": [{
                "FromAge": "0",
                "ToAge": "12",
                "Price": "10"
            }]
        }
    }
}

'Child' in Data_Array.Private.Price ? console.log('Child detected') : console.log('Child missing')

创建变量child

var Data_Array = {
    "Private": {
        "Price": {
            "Adult": "18",
            "Child": [{
                "FromAge": "0",
                "ToAge": "12",
                "Price": "10"
            }]
        }
    }
}

var child = 'Child' in Data_Array.Private.Price && Data_Array.Private.Price.Child[0] || 'there is no child'

console.log(child)

如果没有子元素

var Data_Array = {
    "Private": {
        "Price": {
            "Adult": "18"
        }
    }
}

var child = 'Child' in Data_Array.Private.Price && Data_Array.Private.Price.Child[0] || 'there is no child'

console.log(child)


这个代码可以正常工作,但你能帮我理解一下它是如何工作的吗?var child = 'Child' in Data_Array.Private.Price && Data_Array.Private.Price.Child[0] || 'there is no child'var child = 'Child' in (Data_Array.Private.Price && Data_Array.Private.Price.Child[0]) ? Data_Array.Private.Price.Child[0] : 'there is no child'; 的区别。 - User97798
2
在表达式 var a = b && c || d 中,如果 b 为真,则返回 c 并跳过 d,因此 a = c, 或者如果 b 为假,则跳过 c 并返回 d,因此 a = d。 - user6748331
太棒了!讲解得很好。 - User97798

3

你可以使用in运算符来检查对象中是否包含指定的属性。

in运算符返回一个布尔值,表示指定的属性是否在指定的对象中。

if ('Child' in Data_Array.Private.Price) {
    // more code
}

如果Private不是一个属性,将会抛出一个异常。 - Lucky Soni
没错,但问题是关于测试Child类。 - Nina Scholz
1
只是想告知一下,因为似乎楼主不明白为什么会出现这些异常。 - Lucky Soni

2

当尝试从未定义的对象中获取属性时,会引发异常。除非您确定结构,否则需要在整个链路中检查每个属性的存在性(和类型)。

使用 Lodash:

if(_.has(Data_Array, 'Private.Price.Child')) {
    if(Array.isArray(Data_Array.Private.Price.Child) && Data_Array.Private.Price.Child.length && Data_Array.Private.Price.Child[0].Price) {
        // Its has a price!
    }
}

0

检查 Child 是否未定义

if (typeof Data_Array.Private.Price.Child!== "undefined") {
    ...
}

或者您可以使用 in

if ("Child" in Data_Array.Private.Price) {
    ...
}

或者你可以使用 underscore.js 的 _.isUndefined(Data_Array.Private.Price.Child)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接