我在字符串中有以下JSON编码对象:
{"loggedin":0,"error_message":"login_failed","success_message":"","username":"","sessionId":""}
如何将其转换为JS中的对象,然后检查其loggedin
属性是0还是1?
答案 0 :(得分:2)
你的意思是:
var json = {"loggedin":0,"error_message":"login_failed","success_message":"","username":"","sessionId":""}
if(json.loggedin == 0) {
// do something
}
else {
// do something else
}
<强>更新强>
如果您将JSON作为字符串,则必须先解析它才能执行此操作。所以也许你必须这样做:
var responseText = JSON.parse(responseText);
答案 1 :(得分:1)
var myJSONObject = {"loggedin":0,"error_message":"login_failed","success_message":"","username":"","sessionId":""};
if (myJSONObject.loggedin === 0) {
// do something
} else {
// do something else
}
答案 2 :(得分:0)