這是我的代碼:獲取JSON響應迅速的值
do {
if let json = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as? NSDictionary {
print(json)
let success = json ["success"] as? Int
print("Success: \(success)")
這是我的輸出:
{
error = "Account does not exist, please create it!";
}
Success: nil
`
所以,let success = json ["success"] as? Int
之前,一切正常,但爲什麼是這條線後的輸出nil
?
這是我的PHP:
public function login($username,$password) {
$query = "Select * from users where username = '$username' and password = '$password'";
$result = mysqli_query($this->connection, $query);
if (mysqli_num_rows($result) > 0) {
$json['success'] = 'Welcome '. $username . '!';
echo json_encode($json);
mysqli_close($this->connection);
} else {
$json['error'] = 'Account does not exist, please create it!';
echo json_encode($json);
mysqli_close($this->connection);
}
我假設你解析的JSON不包含關鍵「成功」,這就是爲什麼它是零。但沒有看到JSON,這只是一個假設。 – Ollie
謝謝,在您的PHP'else'語句中,您不會爲「成功」關鍵字返回值,這就是爲什麼它是零。 – Ollie
哦,那就對了。謝謝! –