Check if a key exists inside a json object

后端 未结 9 1379
旧巷少年郎
旧巷少年郎 2020-11-29 14:48
amt: \"10.00\"
email: \"sam@gmail.com\"
merchant_id: \"sam\"
mobileNo: \"9874563210\"
orderID: \"123456\"
passkey: \"1234\"

The above is the JSON o

9条回答
  •  借酒劲吻你
    2020-11-29 15:30

    There's several ways to do it, depending on your intent.

    thisSession.hasOwnProperty('merchant_id'); will tell you if thisSession has that key itself (i.e. not something it inherits from elsewhere)

    "merchant_id" in thisSession will tell you if thisSession has the key at all, regardless of where it got it.

    thisSession["merchant_id"] will return false if the key does not exist, or if its value evaluates to false for any reason (e.g. if it's a literal false or the integer 0 and so on).

提交回复
热议问题