json_decode() - 语法错误

时间:2016-02-11 18:22:20

标签: php json

我想删除特殊字符(并在论坛中找到):

public class TimerClass {

  private Timer timer;
  private TimerTask timerTask;
  private int time;

 public TimerClass(){
    timer = new Timer();
    time = 0;
 }

 public void createTimer(){

 }

 public void runTimer(){

 }
 public int getTime(){
    return this.time;
 }
 public void deleteTimer(){

 }
 public void endTimer(){

 }
}

同样的

$response = trim(preg_replace("#(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/)|([\s\t]//.*)|(^//.*)#", '', $response));

这用于编码

for ($i = 0; $i <= 31; ++$i) {
    $response = str_replace(chr($i), "", $response);
}

$response = str_replace(chr(127), "", $response);

if (0 === strpos(bin2hex($response), 'efbbbf')) {
    $response = substr($response, 3);
}

此时$response = mb_convert_encoding($response, "UTF-8"); echo "\nJSON Response:#$response#\n"; 回应:

$response

最后

{"data":{"taxa":[{"placa":"EDY8986","taxas_detran":"141.36","seguro_dpvat":"211.30","ipva":"1945.20","multas":"5048.10","total_debitos":"null"}]},"code":200,"pagination":{"rows":1,"page":1,"pages":0,"hasNext":false,"totalRows":1}}

$data = json_decode('"' . $response . '"',true, 512); echo "\n\nData>\n"; print_r($data); echo "\nError> "; echo json_last_error_msg(); 打印:

  

语法错误

我已经在JSONLint和JSON Formatter中对它进行了验证,它是有效的。

2 个答案:

答案 0 :(得分:2)

你需要改变一点点才能正常工作: -

$data = json_decode($response,true, 512);// remove quotes

答案 1 :(得分:1)

你用额外的引号杀死你的JSON:

 $data = json_decode('"' . $response . '"',true, 512);
                      ^-----------------^

假设您的$回复是

{"foo":"bar"}

然后你要制作/传递

"{"foo":"bar"}"

这是一个彻头彻尾的JSON语法错误:

"{"foo":"bar"}"
^--start string
  ^-end string
   ^^^---????