douqianxian7008 2012-11-27 11:12
浏览 141
已采纳

json_decode返回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. I think it's because of UTF-8 BOM. Any Solution ? Im using Windows7 OS with xampp. I set my Encoding to

header('Content-type:application/json; charset=utf-8');

JSON DATA

{"command":"E101","user_id":"someuser","movie_id":"1","link_id":"2"}

JSON Error: Control character error, possibly incorrectly encoded

 $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

 {"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 . Why this happened ?

JSON Data Bin2Hex() 7b22636f6d6d616e64223a2245313031222c226d6f7669655f6964223a226d6f7669655f6964222c226c696e6b5f6964223a226c696e6b5f6964227d00000000

Im Encrypting the JSON data and via client side, im decrypting at the server side.

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;
}
  • 写回答

2条回答 默认 最新

  • dongsheng1238 2012-11-27 13:50
    关注

    Your decryption has apparently left a bunch of padding NUL bytes at the end of the string.

    Either fix your decryption mechanism or trim them: trim($json, "\x0")

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)