douzhuo6931 2016-10-15 02:10
浏览 110
已采纳

json_decode()不起作用

I am using ajax to send data with JSON and it keeps returning null. Here is the string I'm sending:

{"username":"HittmanA","code":"%601234567890-%3D~!%40%23%24%25%5E%26*()_%2Bqwertyuiop%5B%5D%5CQWERTYUIOP%7B%7D%7Casdfghjkl%3B'ASDFGHJKL%22zxcvbnm%2C%2FZXCVBNM%3C%3E%3F","name":"Untitled-1"}

It is sent via post. Here is the code I send it with:

       function slash(strr){
                            var re = /([^a-zA-Z0-9])/g; 
                            var str = strr;
                            var subst = '\$1'; 
                            var st = encodeURIComponent(str.replace(re,subst));
                            console.log("st");
                            return st;
                           }
          function create() {
            var info = {};
            var code=editor.getValue();
            info.username=username;
            info.code=slash(code);
            var name=document.getElementById('projectName').value;
            name2=name;
            info.name=slash(name2);
            info=JSON.stringify(info);
            console.log(info);
            var xhttp = new XMLHttpRequest();
            xhttp.onreadystatechange = function() {
              if (xhttp.readyState == 4 && xhttp.status == 200) {
                document.getElementById("demo").innerHTML = xhttp.responseText;
              }
            };
            xhttp.open("POST", "create_project.php", true);
            xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            xhttp.send("info="+info);
          }

When it gets received in the php file it is processed like this:

    $info = $_POST['info'];
    echo "<pre>".$info."</pre>";
    //$info = urldecode($info);
    $info = json_decode($info);
    echo "<pre>".$info."</pre>";

However for some reason the json_decode() doest work. Again here is the JSON I'm sending:

{"username":"HittmanA","code":"%601234567890-%3D~!%40%23%24%25%5E%26*()_%2Bqwertyuiop%5B%5D%5CQWERTYUIOP%7B%7D%7Casdfghjkl%3B'ASDFGHJKL%22zxcvbnm%2C%2FZXCVBNM%3C%3E%3F","name":"Untitled-1"}

the first echo works correctly but the second one doesn't. How do I fix this?

  • 写回答

5条回答 默认 最新

  • douhong6187 2016-10-15 02:53
    关注

    json_decode() must be emitting an error which you are not checking. Functions like json_decode() and json_encode() do not display errors, you must use json_last_error and since PHP 5.5 there is also json_last_error_msg().

    <?php
    
    $str = '{"username":"HittmanA","code":"%601234567890-%3D~!%40%23%24%25%5E%26*()_%2Bqwertyuiop%5B%5D%5CQWERTYUIOP%7B%7D%7Casdfghjkl%3B\'ASDFGHJKL%22zxcvbnm%2C%2FZXCVBNM%3C%3E%3F","name":"Untitled-1"}';
    
    var_dump($str);
    var_dump(json_decode($str));
    var_dump(json_last_error());
    var_dump(json_last_error_msg());
    

    The above outputs:

    string(189) "{"username":"HittmanA","code":"%601234567890-%3D~!%40%23%24%25%5E%26*()_%2Bqwertyuiop%5B%5D%5CQWERTYUIOP%7B%7D%7Casdfghjkl%3B\'ASDFGHJKL%22zxcvbnm%2C%2FZXCVBNM%3C%3E%3F","name":"Untitled-1"}"
    class stdClass#1 (3) {
      public $username =>
      string(8) "HittmanA"
      public $code =>
      string(136) "%601234567890-%3D~!%40%23%24%25%5E%26*()_%2Bqwertyuiop%5B%5D%5CQWERTYUIOP%7B%7D%7Casdfghjkl%3B\'ASDFGHJKL%22zxcvbnm%2C%2FZXCVBNM%3C%3E%3F"
      public $name =>
      string(10) "Untitled-1"
    }
    int(0)
    string(8) "No error"
    

    If we try to decode invalid JSON:

    <?php
    
    $str = 'foobar{';
    
    var_dump($str);
    var_dump(json_decode($str));
    var_dump(json_last_error());
    var_dump(json_last_error_msg());
    

    The above prints:

    string(7) "foobar{"
    NULL
    int(4)
    string(16) "boolean expected"
    

    There must be an error in the JSON when you try to decode it. Check for errors usings the json_* error message functions. The error message will tell you what's wrong and it will be straight to fix once you know what the error is.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)