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 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab