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);
    }
}
1
  • Can you show us more of your code, there should be nothing wrong so far with this... Commented Sep 21, 2012 at 4:53

2 Answers 2

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);
Sign up to request clarification or add additional context in comments.

1 Comment

My Server is 5.2 last_error() will not work on it
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.