json_decode错误

时间:2011-07-22 15:40:02

标签: php json

我在使用PHP中的json_decode()时遇到问题。我使用json2.js library将JSON转换为字符串。然后将其发布到PHP。那部分似乎很好。

这是我的PHP函数:

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
}

以下内容将返回以下内容:

Intrant : 
{"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":""}
VAR DUMP:
NULL

4 个答案:

答案 0 :(得分:3)

以下对我来说效果很好:

<?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);

如果这对您不起作用(作为PHP脚本本身),那么您可能没有安装PHP的json扩展。使用“php -m | grep json”或function_exists(“json_decode”)进行检查。

答案 1 :(得分:2)

不知道你在做什么..

但完美地在这里工作

http://pastebin.com/DzSs8mNd

答案 2 :(得分:1)

您的json字符串(由浏览器显示)正确解析。我想我们中的一些人已经看到了这一点。但是,浏览器显示的内容以及真正的内容往往是非常不同的东西。例如,您可能在错误的位置隐藏了空白字符。尝试移动<pre>,看看会发生什么:

print '<pre>Intrant : <br />'.$json.'<br />';
print 'VAR DUMP:<br />';
var_dump($json2);
print '</pre>';

答案 3 :(得分:1)

感谢Marc B, 由于我们的服务器使用charset ISO-8859-1,因此json_decode函数不起作用。

$json2 = json_decode(utf8_encode($json),true);

全部谢谢