Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

json_decode return NULL , UTF-8 BOM

I want to decode the JSON data and store it in to an array using json_decode function but it's return NULL Value. I think it's because of UTF-8 BOM. Any Solution ? Im using Windows7 OS with xampp. I set my Encoding to

header('Content-type:application/json; charset=utf-8');

JSON DATA

{"command":"E101","user_id":"someuser","movie_id":"1","link_id":"2"}

JSON Error: Control character error, possibly incorrectly encoded

 $json_errors = array(
     JSON_ERROR_NONE => 'No error has occurred',
     JSON_ERROR_DEPTH => 'The maximum stack depth has been exceeded',
     JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded',
     JSON_ERROR_SYNTAX => 'Syntax error',
    );
    echo 'Last error : ', $json_errors[json_last_error()], PHP_EOL, PHP_EOL;

IF I Parse this JSON, No error Occured

 {"command":"E101","user_id":"someuser","movie_id":"movie_id","link_id":"link_id"}

The Only difference is Im storing string data in to movie_id and link_id . Why this happened ?

JSON Data Bin2Hex() 7b22636f6d6d616e64223a2245313031222c226d6f7669655f6964223a226d6f7669655f6964222c226c696e6b5f6964223a226c696e6b5f6964227d00000000

Im Encrypting the JSON data and via client side, im decrypting at the server side.

Here goes my Encryption function

    public function ajax_enc($data){

    $vector = "myvector";
    $filter = new Zend_Filter_Encrypt(array('adapter' => 'mcrypt', 'key' => $this->_AJAXKEY));
    $filter->setVector($vector);
    $encrypted = $filter->filter($data);
    // bin2hex for user use case     
    return bin2hex($encrypted); // rawurlencode(..) works

    }

Decrypt

public function ajax_dec($data)
{
$vector = "myvector";
$filter = new Zend_Filter_Decrypt(array('adapter' => 'mcrypt', 'key' => $this->_AJAXKEY ));
$filter->setVector($vector);
$decoded = pack('H*', $data);
$decrypted = $filter->filter($decoded);
return $decrypted;
}
like image 914
Navneet Singh Avatar asked Nov 27 '12 11:11

Navneet Singh


People also ask

Why is json_decode returning NULL?

If json_decode returns null is it because the database. json is not valid.

What does json_decode return?

The json_decode() function can return a value encoded in JSON in appropriate PHP type. The values true, false, and null is returned as TRUE, FALSE, and NULL respectively. The NULL is returned if JSON can't be decoded or if the encoded data is deeper than the recursion limit.

What is the difference between Json_encode and json_decode?

I think json_encode makes sure that php can read the . json file but you have to specify a variable name, whereas with json_decode it's the same but you have to specify a file name.

What is the use of json_decode?

The json_decode() function is used to decode or convert a JSON object to a PHP object.


1 Answers

Your decryption has apparently left a bunch of padding NUL bytes at the end of the string.

Either fix your decryption mechanism or trim them: trim($json, "\x0")

like image 145
deceze Avatar answered Sep 24 '22 21:09

deceze