php isJson方法php版本错误

时间:2014-02-17 15:53:38

标签: php

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

实际上,json_last_error正在开发PHP 5.3及更高版本,但现在我将使用PHP 5.2 Server,因此这种方法不起作用。请告诉我任何其他选择

1 个答案:

答案 0 :(得分:0)

如果json字符串无法解码,json_decode将返回NULL,因此可以查看:

function isJson($string) {
 return json_decode($string) != null;
}