PHP: json_decode not working

    |
  • Added:
  • |
  • In: Basic PHP

This does not work:

$jsonDecode = json_decode($jsonData, TRUE); 

However if I copy the string from $jsonData and put it inside the decode function manually it does work.

This works:

$jsonDecode = json_decode('{"id":"0","bid":"918","url":"http:\/\/www.google.com","md5":"6361fbfbee69f444c394f3d2fa062f79","time":"2014-06-02 14:20:21"}', TRUE); 

I did output $jsonData copied it and put in like above in the decode function. Then it worked. However if I put $jsonData directly in the decode function it does not.

var_dump($jsonData) shows:

string(144) "{"id":"0","bid":"918","url":"http:\/\/www.google.com","md5":"6361fbfbee69f444c394f3d2fa062f79","time":"2014-06-02 14:20:21"}" 

The $jsonData comes from a encrypted $_GET variable. To encrypt it I use this:

$key = "SOME KEY"; $iv_size = mcrypt_get_iv_size(MCRYPT_BLOWFISH, MCRYPT_MODE_ECB); $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); $enc = mcrypt_encrypt(MCRYPT_BLOWFISH, $key, $data, MCRYPT_MODE_ECB, $iv); $iv = rawurlencode(base64_encode($iv)); $enc = rawurlencode(base64_encode($enc)); //To Decrypt $iv = base64_decode(rawurldecode($_GET['i'])); $enc = base64_decode(rawurldecode($_GET['e'])); $data = mcrypt_decrypt(MCRYPT_BLOWFISH, $key, $enc, MCRYPT_MODE_ECB, $iv); 
This Question Has 6 Answeres | Orginal Question | yoshi

Judging from the other comments, you could use,

$jsonDecode = json_decode(trim($jsonData), TRUE);

str_replace("\t", " ", str_replace("\n", " ", $string))

because json_decode does not work with special characters. And no error will be displayed. Make sure you remove tab spaces and new lines. Depending on the source you get your data, you might need also: stripslashes(html_entity_decode($string))

Works for me:

<?php $sql = <<<EOT SELECT * FROM `students`; EOT; $string = '{ "query" : "' . str_replace("\t", " ", str_replace("\n", " ", $sql)).'" }'; print_r(json_decode($string)); ?> 

output:

stdClass Object ( [query] => SELECT * FROM `students`; ) 

some time there is issue of html entities, for example \" it will represent like this \&quot, so you must need to parse the html entites to real text, that you can do using html_entity_decode() method of php.

$jsonData = stripslashes(html_entity_decode($jsonData)); $k=json_decode($jsonData,true); print_r($k); 

Interestingly mcrypt_decrypt seem to add control characters other than \0 at the end of the resulting text because of its padding algorithm. Therefore instead of rtrim($jsonData, "\0") it is recommended to use

preg_replace( "/\p{Cc}*$/u", "", $data) 

on the result $data of mcrypt_decrypt. json_decode will work if all trailing control characters are removed. Pl refer to the comment by Peter Bailey at http://php.net/manual/en/function.mdecrypt-generic.php .

Be sure to set header to JSON

header('Content-type: application/json;'); 

Most likely you need to strip off the padding from your decrypted data. There are 124 visible characters in your string but var_dump reports 144. Which means 20 characters of padding needs to be removed (a series of "\0" bytes at the end of your string).

Probably that's 4 "\0" bytes at the end of a block + an empty 16-bytes block (to mark the end of the data).

How are you currently decrypting/encrypting your string?

Edit:

You need to add this to trim the zero bytes at the end of the string:

$jsonData = rtrim($jsonData, "\0"); 

Search
I am...

Sajjad Hossain

I have five years of experience in web development sector. I love to do amazing projects and share my knowledge with all.

NEED HOSTING?
Are you searching for Good Quality hosting?
You can Try It!
Connect Social With PHPAns
Top