[英]json_decode() not working
我正在使用 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"}
第一個回聲正常工作,但第二個沒有。 我該如何解決這個問題?
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_*
錯誤消息函數檢查錯誤。 錯誤消息會告訴您出了什么問題,一旦您知道錯誤是什么,它就會直接修復。
你好先生,如果你在 php 中 echo 等於 return 或 print 在一些函數式編程中。 如果您使用 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>";
像這樣使用 json_decode:
$info = json_decode($info);
將返回一個 PHP 變量。
如果將關聯參數添加為 true(默認為 false),如下所示:
$info = json_decode($info, true);
然后它將返回一個關聯數組
也許將 xhttp 內容類型設置為 json 就像
<?PHP
$data = /** whatever you're serializing **/;
header('Content-Type: application/json');
echo json_encode($data); ?>
如本答案所述。
查看該對象,您的代碼參數中似乎有一個 ' 。 我認為這對編碼無效。
我在我的 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 服務器,它開始正常工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.