amt: "10.00"
email: "sam@gmail.com"
merchant_id: "sam"
mobileNo: "9874563210"
orderID: "123456"
passkey: "1234"
以上是我正在处理的 JSON 对象。我想检查 merchant_id
键是否存在。我尝试了下面的代码,但它不起作用。有什么方法可以实现吗?
<script>
window.onload = function getApp()
{
var thisSession = JSON.parse('<?php echo json_encode($_POST); ?>');
//console.log(thisSession);
if (!("merchant_id" in thisSession)==0)
{
// do nothing.
}
else
{
alert("yeah");
}
}
</script>
最佳答案
试试这个,
if(thisSession.hasOwnProperty('merchant_id')){
}
JS 对象 thisSession
应该像
{
amt: "10.00",
email: "sam@gmail.com",
merchant_id: "sam",
mobileNo: "9874563210",
orderID: "123456",
passkey: "1234"
}
你可以找到详细信息here
关于javascript - 检查 JSON 对象中是否存在键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20804163/