dpfz27768 2013-06-20 17:22
浏览 901

json_decode返回JSON_ERROR_SYNTAX但在线格式化程序说JSON没问题

I got a very strange problem.

I have a JSON webservice.

When i check it with this website http://www.freeformatter.com/json-formatter.html#ad-output

Everything is OK.

But when i load my JSON with this code :

  $data = file_get_contents('http://www.mywebservice');

if(!empty($data))
{

    $obj = json_decode($data);

 switch (json_last_error()) {
    case JSON_ERROR_NONE:
        echo ' - JSON_ERROR_NONE';
    break;
    case JSON_ERROR_DEPTH:
        echo ' - JSON_ERROR_DEPTH';
    break;
    case JSON_ERROR_STATE_MISMATCH:
        echo ' - JSON_ERROR_STATE_MISMATCH';
    break;
    case JSON_ERROR_CTRL_CHAR:
        echo ' -  JSON_ERROR_CTRL_CHAR';
    break;
    case JSON_ERROR_SYNTAX:
        echo "

 - SYNTAX ERROR 

";
    break;
    case JSON_ERROR_UTF8:
        echo ' - JSON_ERROR_UTF8';
    break;
    default:
        echo ' - Unknown erro';
    break;
}

I got the error : SYNTAX ERROR

WHICH IS NOT HELP FULL AT ALL.

It is a nightmare.

I see that with PHP 5.5 i could use this function : http://php.net/manual/en/function.json-last-error-msg.php

(but i did not succeed to install PHP 5.5 yet, and i m not sure this function will give me more detail)

  • 写回答

11条回答 默认 最新

  • douwo4837 2013-06-20 17:30
    关注

    You haven't show your JSON but this sound like it could be an Invalid UTF-8 sequence in argument, most online validator wont catch it. make sure your data is UTF-8 and also check if you have foreign characters. You don't need PHP5 to see your error, use error_log() to log the problems.

    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料