[英]json_decode return NULL , UTF-8 BOM
I want to decode the JSON data and store it in to an array using json_decode function but it's return NULL Value. 我想解码JSON数据并使用json_decode函数将其存储到数组中,但它返回NULL值。 I think it's because of UTF-8 BOM. 我认为这是因为UTF-8 BOM。 Any Solution ? 任何方案? Im using Windows7 OS with xampp. 我在xampp上使用Windows7操作系统。 I set my Encoding to 我将我的编码设置为
header('Content-type:application/json; charset=utf-8');
JSON DATA JSON数据
{"command":"E101","user_id":"someuser","movie_id":"1","link_id":"2"}
JSON Error: Control character error, possibly incorrectly encoded JSON错误:控制字符错误,可能编码错误
$json_errors = array(
JSON_ERROR_NONE => 'No error has occurred',
JSON_ERROR_DEPTH => 'The maximum stack depth has been exceeded',
JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded',
JSON_ERROR_SYNTAX => 'Syntax error',
);
echo 'Last error : ', $json_errors[json_last_error()], PHP_EOL, PHP_EOL;
IF I Parse this JSON, No error Occured 如果我解析这个JSON,没有错误发生
{"command":"E101","user_id":"someuser","movie_id":"movie_id","link_id":"link_id"}
The Only difference is Im storing string data in to movie_id and link_id . 唯一的区别是我将字符串数据存储到movie_id和link_id中。 Why this happened ? 为什么会这样?
JSON Data Bin2Hex() 7b22636f6d6d616e64223a2245313031222c226d6f7669655f6964223a226d6f7669655f6964222c226c696e6b5f6964223a226c696e6b5f6964227d00000000 JSON数据Bin2Hex()7b22636f6d6d616e64223a2245313031222c226d6f7669655f6964223a226d6f7669655f6964222c226c696e6b5f6964223a226c696e6b5f6964227d00000000
Im Encrypting the JSON data and via client side, im decrypting at the server side. 我加密JSON数据,并通过客户端,我在服务器端解密。
Here goes my Encryption function 这是我的加密功能
public function ajax_enc($data){
$vector = "myvector";
$filter = new Zend_Filter_Encrypt(array('adapter' => 'mcrypt', 'key' => $this->_AJAXKEY));
$filter->setVector($vector);
$encrypted = $filter->filter($data);
// bin2hex for user use case
return bin2hex($encrypted); // rawurlencode(..) works
}
Decrypt 解码
public function ajax_dec($data)
{
$vector = "myvector";
$filter = new Zend_Filter_Decrypt(array('adapter' => 'mcrypt', 'key' => $this->_AJAXKEY ));
$filter->setVector($vector);
$decoded = pack('H*', $data);
$decrypted = $filter->filter($decoded);
return $decrypted;
}
Your decryption has apparently left a bunch of padding NUL bytes at the end of the string. 您的解密显然在字符串末尾留下了一堆填充NUL字节。
Either fix your decryption mechanism or trim them: trim($json, "\\x0")
修复你的解密机制或修剪它们: trim($json, "\\x0")
To remove 
do: 删除
做:
$json_raw_str = ltrim($json_raw_str, chr(239).chr(187).chr(191));
Why? 为什么? Because Byte_order_mark 239 187 191
is the decimal representation of 
and ltrim
remove them from the begining of the string. 因为Byte_order_mark 239 187 191
是
的十进制表示,并且ltrim
从字符串的ltrim
删除它们。
After this do: 在这之后:
$data = json_decode($json_raw_str);
// be fun :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.