简体   繁体   English

json_decode() 不起作用

[英]json_decode() not working

I am using ajax to send data with JSON and it keeps returning null.我正在使用 ajax 用 JSON 发送数据,它一直返回 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:当它在 php 文件中收到时,它的处理方式如下:

    $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.但是由于某种原因 json_decode() 不起作用。 Again here is the JSON I'm sending:这里再次是我发送的 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"}

the first echo works correctly but the second one doesn't.第一个回声正常工作,但第二个没有。 How do I fix this?我该如何解决这个问题?

json_decode() must be emitting an error which you are not checking. json_decode()必须发出一个您没有检查的错误。 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() . 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());

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:如果我们尝试解码无效的 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.当您尝试解码时,JSON 中肯定有错误。 Check for errors usings the json_* error message functions.使用json_*错误消息函数检查错误。 The error message will tell you what's wrong and it will be straight to fix once you know what the error is.错误消息会告诉您出了什么问题,一旦您知道错误是什么,它就会直接修复。

hello mister if you echo in php is equal to return or print in some functional programming.你好先生,如果你在 php 中 echo 等于 return 或 print 在一些函数式编程中。 if your using ajax, ajax is one way communication.如果您使用 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>";

Using json_decode like this:像这样使用 json_decode:

$info = json_decode($info);

Will return a PHP variable.将返回一个 PHP 变量。

If you add the associative parameter as true (false by default) like this:如果将关联参数添加为 true(默认为 false),如下所示:

$info = json_decode($info, true);

Then it will return an associative array然后它将返回一个关联数组

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

Perhaps setting the xhttp content-type as json like也许将 xhttp 内容类型设置为 json 就像

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

As stated in this answer.如本答案所述。

Looking at the object it looks like you have a ' inside your code parameter.查看该对象,您的代码参数中似乎有一个 ' 。 I think that's invalid for encoding.我认为这对编码无效。

I was struggling with this on my Windows 10 machine, PHP version 5.5.9 only to find that the php_json.dll file was not in the ext folder of my installation and obviously no extension to uncomment in php.ini file.我在我的 Windows 10 机器上苦苦挣扎,PHP 版本 5.5.9 只是发现 php_json.dll 文件不在我安装的 ext 文件夹中,而且显然没有扩展名可以在 php.ini 文件中取消注释。 I found the dll at http://originaldll.com/file/php_json.dll/31989.html .我在http://originaldll.com/file/php_json.dll/31989.html找到了 dll。 After copying it to my ext folder I added extension=php_json.dll to my php.ini file and restarted my apache server and it started working fine.将它复制到我的 ext 文件夹后,我将 extension=php_json.dll 添加到我的 php.ini 文件并重新启动了我的 apache 服务器,它开始正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM