1

I want to remove special chars (and found this in a forum):

$response = trim(preg_replace("#(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/)|([\s\t]//.*)|(^//.*)#", '', $response));

The same for this

for ($i = 0; $i <= 31; ++$i) {
    $response = str_replace(chr($i), "", $response);
}

$response = str_replace(chr(127), "", $response);

if (0 === strpos(bin2hex($response), 'efbbbf')) {
    $response = substr($response, 3);
}

And this for encoding

$response = mb_convert_encoding($response, "UTF-8");
echo "\nJSON Response:#$response#\n";

At this point $response echoes:

{"data":{"taxa":[{"placa":"EDY8986","taxas_detran":"141.36","seguro_dpvat":"211.30","ipva":"1945.20","multas":"5048.10","total_debitos":"null"}]},"code":200,"pagination":{"rows":1,"page":1,"pages":0,"hasNext":false,"totalRows":1}}

In the end

$data = json_decode('"' . $response . '"',true, 512);
echo "\n\nData>\n";
print_r($data);
echo "\nError> "; echo json_last_error_msg();

json_last_error_msg() prints:

Syntax Error

I've already validated it in JSONLint and JSON Formatter and it is valid.

2
  • 2
    $data = json_decode($response,true, 512); Commented Feb 11, 2016 at 18:23
  • I can't believe it. Maybe i get crazy. Thanks man, its working! Commented Feb 11, 2016 at 18:26

2 Answers 2

2

You need to just littile bit change and it work fine:-

$data = json_decode($response,true, 512);// remove quotes
Sign up to request clarification or add additional context in comments.

Comments

1

You're killing your JSON with the extra quotes:

 $data = json_decode('"' . $response . '"',true, 512);
                      ^-----------------^

Assuming your $response is

{"foo":"bar"}

Then you'd be producing/passing

"{"foo":"bar"}"

which is an outright JSON syntax error:

"{"foo":"bar"}"
^--start string
  ^-end string
   ^^^---????

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.