使用javascript解析JSON并在数组中获取特定值

使用javascript解析JSON并在数组中获取特定值,javascript,php,arrays,json,parsing,Javascript,Php,Arrays,Json,Parsing,在我的控制台日志中,阵列如下所示: {"userid":"5502","fullname":"My fullname","email":"sample@yahoo.com","user_access":"real"} if (access == '"real"') { // What should be the format of access variable? alert("Welcome"); location.href = "home.html"; } 现在在我的aj

在我的控制台日志中,阵列如下所示:

{"userid":"5502","fullname":"My fullname","email":"sample@yahoo.com","user_access":"real"}
if (access == '"real"') { // What should be the format of access variable?
    alert("Welcome");
    location.href = "home.html";
}
现在在我的ajax上,我有一个用于服务器发送到应用程序的数据数组的句柄代码:

function handleData(responseData) {
    var access = responseData;

    console.log(access);
    if (access == '"real"') {
        alert("Welcome");
        location.href = "home.html";
    } else {
        alert("Your username and password didn\'t match.");
    }
}
如何在数组中获取此
“user\u access”:“real”
的特定值并在条件下使用它

像这样:

{"userid":"5502","fullname":"My fullname","email":"sample@yahoo.com","user_access":"real"}
if (access == '"real"') { // What should be the format of access variable?
    alert("Welcome");
    location.href = "home.html";
}

通常,我们希望我们的响应是json(或者我们可以说“object”)形式,这样我们就可以轻松访问它的内部属性。因此,如果它已经是一个对象,则不需要使用
JSON.parse
。您可以直接访问这样的任何属性-
responseData.user\u access
。但是如果它是字符串形式,那么您必须首先使用
JSON.parse()
将字符串解析为JSON(或对象)格式。

如果它在{}括号中没有被“”包围,那么只需执行以下操作即可

function handleData(responseData) {
    var access = responseData.access;
    if (access === 'real') {
        alert("Welcome");
        location.href = "home.html";
    } else {
        alert("Your username and password didn\'t match.");
    }
}

如果(access.access==“real”)..
responseData.access===“real”
这不是数组而是对象..用于访问
对象的属性
您的问题(示例对象)不准确。当您实际需要
用户访问权限
属性时,您询问了如何检查
访问权限
属性。@evolution,是的,我输入代码中使用的真实对象时出错了。无论如何,谢谢你。console.log(access);它是未定义的。请粘贴这两行的输出:console.log(responseData);控制台日志(响应数据类型);console.log(responseData);-->{“用户id”:“5502”,“用户全名”:“我的全名”,“用户电子邮件”:sample@yahoo.com,“user_access”:“real”}console.log(typeof responseData);-->stringok,因此它没有
访问权
属性,而是
用户访问权
。我已相应地更新了我的答案。请再试一次。嗨@Mike,对不起,我刚回去工作。现在开始工作了,谢谢你帮我。我的错误我在我的例子上写错了对象。它应该是用户访问而不是访问。当我试图在控制台日志中显示(访问)它时,它未定义。