0

Possible Duplicate:
json_decode returns NULL after webservice call


I am developing a REST service oriented App, in that on the place of login , i post the username and password it returns a json of following

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

after that when i try to json_decode(), it showing NULL, can you tell me what is the problem my server is PHP 5.2.12 and it support 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);
    }
}
Burhan Ali
  • 2,258
  • 1
  • 28
  • 38
Alex Mathew
  • 1,534
  • 9
  • 31
  • 57

2 Answers2

2

Your JSON validates using JSONLint. It might be unwanted spaces or characters that gets passed via the url. I think if you were to add the clean JSON string this problem might no occur. Try adding json_last_error() after json_decode() to determine the exact problem. It should be something like:

        $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);
Conrad Lotz
  • 8,200
  • 3
  • 23
  • 27
0

Look onto this links

PHP json_decode() returns NULL with valid JSON?

json_decode() returns null issues

json_decode returns NULL after webservice call

I think it is because of UTF-8 BOM

if(get_magic_quotes_gpc()){
  $d = stripslashes($result);
}else{
  $d = $result;
}
$d = json_decode($d,true);

try in this format.

Community
  • 1
  • 1
Yogesh Suthar
  • 30,424
  • 18
  • 72
  • 100