【问题标题】:json_decode() not workingjson_decode() 不工作
【发布时间】:2017-02-24 12:58:18
【问题描述】:

我正在使用 ajax 使用 JSON 发送数据,但它一直返回 null。这是我要发送的字符串:

{"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"}

它是通过邮寄方式发送的。这是我发送给它的代码:

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

当它在 php 文件中被接收时,它的处理方式如下:

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

但是由于某种原因 json_decode() 不起作用。这里又是我要发送的 JSON:

{"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"}

第一个回声正常工作,但第二个没有。我该如何解决这个问题?

【问题讨论】:

    标签: php json ajax syntax


    【解决方案1】:

    json_decode() 必须发出一个您没有检查的错误。 json_decode()json_encode() 之类的函数不会显示错误,您必须使用 json_last_error 并且从 PHP 5.5 开始还有 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());
    

    以上输出:

    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"
    

    如果我们尝试解码无效的 JSON:

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

    以上打印:

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

    当您尝试对其进行解码时,JSON 中一定有错误。使用json_* 错误消息函数检查错误。错误消息会告诉您出了什么问题,一旦您知道错误是什么,它将直接修复。

    【讨论】:

    • 嗯,那为什么我的 son_decode() 不起作用?它返回 NULL
    • 您需要从最简单的情况开始调试它(使用硬编码的已知值),例如当您将$info = $_POST['info']; 替换为$info = '{"username":"HittmanA"}'; 时,这行得通吗?是的?那么,如果从客户端而不是xhttp.send("info="+info); 尝试通过xhttp.send() 发送该硬编码值,它仍然有效吗?然后,像这样继续向后调试以找到错误。
    【解决方案2】:

    您好,先生,如果您在 php 中回显等于在某些函数式编程中返回或打印。 如果您使用 ajax,ajax 是一种通信方式。

       $info = $_POST['info'];
        //this code will be return 
        echo "<pre>".$info."</pre>";
        //this also will not be triggered cause you already return the above code
        //$info = urldecode($info);
        $info = json_decode($info);
        echo "<pre>".$info."</pre>";
    

    【讨论】:

    • 这不起作用。我删除了第一个回声,但它不起作用。
    • $info = json_decode($info, true);那么你应该放一些标题,让它真正返回纯 json 文件,将内容设置为 application/json
    【解决方案3】:

    也许将 xhttp 内容类型设置为 json 之类的

    <?PHP
    $data = /** whatever you're serializing **/;
    header('Content-Type: application/json');
    echo json_encode($data); ?>
    

    如本答案所述。

    【讨论】:

    • 我用javascript而不是php发送JSON。
    • 是的,但我说的是标题类型
    【解决方案4】:

    查看对象,您的代码参数中似乎有一个 '。我认为这对编码无效。

    【讨论】:

    • 我认为你在正确的道路上,但我应该如何编码 ' ?
    • 它来自哪里?或者你为什么要把它放在那里?
    • 一个单独的 .js 文件
    【解决方案5】:

    像这样使用 json_decode:

    $info = json_decode($info);
    

    将返回一个 PHP 变量。

    如果将关联参数添加为 true(默认为 false),如下所示:

    $info = json_decode($info, true);
    

    然后它会返回一个关联数组

    http://php.net/manual/en/function.json-decode.php

    【讨论】:

      【解决方案6】:

      我在我的 Windows 10 机器上苦苦挣扎,PHP 版本 5.5.9 只是发现 php_json.dll 文件不在我安装的 ext 文件夹中,并且显然没有扩展名来取消注释 php.ini 文件。我在http://originaldll.com/file/php_json.dll/31989.html 找到了 dll。将它复制到我的 ext 文件夹后,我将 extension=php_json.dll 添加到我的 php.ini 文件中并重新启动了我的 apache 服务器,它开始正常工作。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-03-26
        • 1970-01-01
        • 1970-01-01
        • 2019-05-17
        • 1970-01-01
        • 2013-08-26
        • 1970-01-01
        相关资源
        最近更新 更多