json_decode 返回的值 如果是null 就不是json格式

解决方案 »

  1.   

    判断 json_decode 是否失败,远比检查 json 语法来的简单json 是 Yaml 的子集,你当然可以利用 php 的 yaml 扩展来实现文法检查。只可惜 windows 系统尚不支持
      

  2.   

    if (is_object(json_decode($s))) 
        { 
           echo 'yes';
        }else{
       echo 'no';
    }
      

  3.   


    function is_json($string) {
     json_decode($string);
     return (json_last_error() == JSON_ERROR_NONE);
    }
      

  4.   

    准确来说,对于未知的字符串或对象使用json_decode来判断是未必可行的。
    比如说:
    var_dump(json_decode( '{"":""}' );
    会出现 段错误而终止脚本的运行
      

  5.   

    $json = json_encode($arr);if (!is_array(json_decode($json,true))){
        echo '';
    }