json_decode() 不起作用 [英] json_decode() not working
问题描述
我正在使用 ajax 用 JSON 发送数据,但它一直返回 null.这是我要发送的字符串:
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);
}
当它在 php 文件中收到时,它的处理方式如下:
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>";
但是由于某种原因 json_decode() 不起作用.这里再次是我发送的 JSON:
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?
推荐答案
json_decode()
必须发出您没有检查的错误.json_decode()
和 json_encode()
等函数不会显示错误,您必须使用 json_last_error
并且从 PHP 5.5 开始还有 json_last_error_msg()
.
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());
以上输出:
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:
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());
以上打印:
string(7) "foobar{"
NULL
int(4)
string(16) "boolean expected"
当您尝试解码时,JSON 中肯定有错误.使用 json_*
错误消息函数检查错误.错误消息会告诉您出了什么问题,一旦您知道错误是什么,它就会直接修复.
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.
这篇关于json_decode() 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!