json_decode error
I'm having issue working with json_decode() in PHP. Im using the json2.js library to convert a JSON to string. Then post it to PHP. That part seems fine.
Here is my PHP function :
public function SaveUser($json){
$json2 = json_decode($json,true);
print 'Intrant : <br />'.$json.'<br />';
print '<pre>VAR DUMP:<br />';
var_dump($json2);
print '</pre>';
// Do some things
}
The following returns the following :
Intrant :
{"user_id":"14","prenom":"pr开发者_开发技巧enom","nom":"nom","profil_heures_fixe":"0","nb_heures_fixe":"","is_userliste":"1","is_paye":"1","username":"username","password":"","telephone":"111-111-1111","cellulaire":"111-111-1111","extension":"30","courriel":"user@server.com","date_embauche":"2017-07-02","machine":"","profil_id":"4","status_id":"1","coordonnees":"","urgence":""}
VAR DUMP:
NULL
The follow works just fine for me:
<?php
function SaveUser($json){
$json2 = json_decode($json,true);
print 'Intrant : <br />'.$json.'<br />';
print '<pre>VAR DUMP:<br />';
var_dump($json2);
print '</pre>';
// Do some things
}
$t = '{"user_id":"14","prenom":"prenom","nom":"nom","profil_heures_fixe":"0","nb_heures_fixe":"","is_userliste":"1","is_paye":"1","username":"username","password":"","telephone":"111-111-1111","cellulaire":"111-111-1111","extension":"30","courriel":"user@server.com","date_embauche":"2017-07-02","machine":"","profil_id":"4","status_id":"1","coordonnees":"","urgence":""}';
SaveUser($t);
If that doesn't work for you (as a PHP script on its own), then it's possible you don't have PHP's json extension installed. Check using either "php -m | grep json" or function_exists("json_decode").
dont know what your doing ..
but works here perfectly
http://pastebin.com/DzSs8mNd
Your json string, as displayed by the browser, parses correctly. I think a few of us have seen that. But, what the browser displays and what is really there are often very different things. For example, you might have a whitespace character hidden in the wrong spot. Try moving the <pre>
and see what happens:
print '<pre>Intrant : <br />'.$json.'<br />';
print 'VAR DUMP:<br />';
var_dump($json2);
print '</pre>';
Thanks Marc B, Since our server is using the charset ISO-8859-1 the json_decode function does not work.
$json2 = json_decode(utf8_encode($json),true);
Thanks all
精彩评论