[英]Parse JSON using javascript and get specific value in array
In my console log the array look like this: 在我的控制台日志中,数组如下所示:
{"userid":"5502","fullname":"My fullname","email":"sample@yahoo.com","user_access":"real"}
Now on my ajax, I have a handle code for the data array that the server sends to the app: 现在在我的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.");
}
}
How I can get the specific value of this "user_access":"real"
in array and use it in condition. 如何在数组中获取此
"user_access":"real"
的特定值并在条件中使用它。
like this: 像这样:
if (access == '"real"') { // What should be the format of access variable?
alert("Welcome");
location.href = "home.html";
}
function handleData(responseData) {
var response = JSON.parse(responseData);//assuming you are getting the response as a string
var access = response.user_access;
console.log(access);
if (access == "real") {
alert("Welcome");
location.href = "home.html";
} else {
alert("Your username and password didn\'t match.");
}
}//handleData()
Normally, we want our response to be in json ( or we can say 'object' ) form, so that we can easily access its inner properties. 通常,我们希望我们的响应以json(或者我们可以说“对象”)的形式出现,以便我们可以轻松地访问其内部属性。 So, if it's already an object, you do not need to use
JSON.parse
. 因此,如果它已经是一个对象,则无需使用
JSON.parse
。 You can directly access any property like this - responseData.user_access
. 您可以直接访问任何属性,例如this-
responseData.user_access
。 But if it's in string form, then you have to use JSON.parse()
first to parse the string into JSON ( or object ) format. 但是,如果采用字符串形式,则必须首先使用
JSON.parse()
将字符串解析为JSON(或object)格式。
If it is not surrounded by "" around the {} brackets then just do 如果它在{}括号中没有被“”包围,则只需
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.");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.