2012-09-21 177 views
0

可能重複:
json_decode returns NULL after webservice callPHP Json_Decode返回null


我正在開發面向應用REST服務,在上登錄的地方,我張貼的用戶名和密碼它返回一個json以下

{"id":"4","username":"sam","redirect":"clients/home"}

後,當我嘗試json_decode(),它顯示NULL,你能告訴我是什麼問題 我的服務器是PHP 5.2.12,它支持JSON_Decode。
function login()
{
$result=$this->rest->request(" http://localhost/account/users/login ","POST");
$qry['result']=json_decode($result);
var_dump($qry['result']);
foreach($qry as $result)
{
$this->session->set_userdata('id',$result->id);
$this->session->set_userdata('username',$result->username);
redirect($result->redirect);
}
}

+0

你能告訴我們更多你的代碼,應該沒有錯誤到目前爲止... – jtheman

+0

顯示更多的代碼。 – Musa

回答

2

你的JSON驗證使用JSONLint。它可能是通過url傳遞的無用空格或字符。我想如果你要添加乾淨的JSON字符串,這個問題可能不會發生。嘗試在json_decode()之後添加json_last_error()以確定確切的問題。它應該是這樣的:

 $qry['result']=json_decode($result); 
     switch(json_last_error()) 
     { 
      case JSON_ERROR_DEPTH: 
       $error = ' - Maximum stack depth exceeded'; 
       break; 
      case JSON_ERROR_CTRL_CHAR: 
       $error = ' - Unexpected control character found'; 
       break; 
      case JSON_ERROR_SYNTAX: 
       $error = ' - Syntax error, malformed JSON'; 
       break; 
      case JSON_ERROR_NONE: 
      default: 
       $error = '';      
     } 
     if (!empty($error)) 
      throw new Exception('JSON Error: '.$error); 
+0

我的服務器是5.2 last_error()將不會進行這項工作 –