Thursday, 15 May 2014

php - json_decode returns JSON_ERROR_SYNTAX but online formatter says the JSON is OK -


I have a very strange problem.

I have a JSON webservice.

When I check it with this website

Everything is fine.

But when I load my code with this code:

  $ data = file_get_contents ('http: //www.mywebservice'); If (! $ Data below) {$ obj = json_decode ($ data); Switch (json_last_error ()) {case JSON_ERROR_NONE: echo '- JSON_ERROR_NONE'; break; Case JSON_ERROR_DEPTH: Echo '- JSON_ERROR_DEPTH'; break; Case JSON_ERROR_STATE_MISMATCH: Echo '- JSON_ERROR_STATE_MISMATCH'; break; Case JSON_ERROR_CTRL_CHAR: Resonant '- JSON_ERROR_CTRL_CHAR'; break; Case JSON_ERROR_SYNTAX: Echo "\ r \ n \ r \ n - SYNTAX error \ r \ n \ r \ n"; break; Case JSON_ERROR_UTF8: Resonance '- JSON_ERROR_UTF8'; break; Default: Echo '- Unknown Arrow'; break; }   

I got the error: SYNTAX error

which could not help at all.

This is a nightmare

I think that with PHP 5.5 I can use this function:

(But I have yet to run PHP 5.5 , And I'm not sure this function will give me more details)

I had to face the same problem, in fact some hidden letters have been ignored and you need to remove it. Here's a global code that works in many cases:

  & lt ;? Php $ checkLogin = file_get_contents ("http://yourwebsite.com/JsonData"); // This will remove the unwanted characters I // Check for details for http://www.php.net/chr ($ i = 0; $ i & lt; = 31; ++ $ i) {$ checkLogin = Str_replace (chr ($ i), "", $ checkLogin); } $ CheckLogin = str_replace (chr (127), "", $ checklogin); // This is the most common part // Some files start with 'FBBBF', so that the start of the file can be marked. (Binary level) // Here we find out and we remove it, basically it is the first 3 letter if (0 === strings (bin2hex ($ checklogin, 'efbbbf')) {$ checkLogin = substr ($ Checklogin, 3); } $ CheckLogin = json_decode ($ check log); Print_r ($ checkLogin); ? & Gt;    

No comments:

Post a Comment