php 如何确定字符串是否是有效的 JSON?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1187576/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to determine whether a string is valid JSON?
提问by Spot
Does anyone know of a robust (and bullet proof) is_JSON function snippet for PHP? I (obviously) have a situation where I need to know if a string is JSON or not.
有谁知道 PHP 的强大(和防弹) is_JSON 函数片段?我(显然)有一种情况,我需要知道一个字符串是否是 JSON。
Hmm, perhaps run it through a JSONLintrequest/response, but that seems a bit overkill.
嗯,也许通过JSONLint请求/响应来运行它,但这似乎有点矫枉过正。
回答by Daff
If you are using the built in json_decodePHP function, json_last_errorreturns the last error (e.g. JSON_ERROR_SYNTAXwhen your string wasn't JSON).
如果您使用内置的json_decodePHP 函数,则json_last_error返回最后一个错误(例如,JSON_ERROR_SYNTAX当您的字符串不是 JSON 时)。
Usually json_decodereturns nullanyway.
通常无论如何都会json_decode返回null。
回答by cgaldiolo
For my projects I use this function (please read the "Note" on the json_decode()docs).
对于我的项目,我使用此函数(请阅读json_decode()文档中的“注释” )。
Passing the same arguments you would pass to json_decode() you can detect specific application "errors" (e.g. depth errors)
传递您将传递给 json_decode() 的相同参数,您可以检测特定的应用程序“错误”(例如深度错误)
With PHP >= 5.6
使用 PHP >= 5.6
// PHP >= 5.6
function is_JSON(...$args) {
json_decode(...$args);
return (json_last_error()===JSON_ERROR_NONE);
}
With PHP >= 5.3
使用 PHP >= 5.3
// PHP >= 5.3
function is_JSON() {
call_user_func_array('json_decode',func_get_args());
return (json_last_error()===JSON_ERROR_NONE);
}
Usage example:
用法示例:
$mystring = '{"param":"value"}';
if (is_JSON($mystring)) {
echo "Valid JSON string";
} else {
$error = json_last_error_msg();
echo "Not valid JSON string ($error)";
}
回答by Pascal MARTIN
What about using json_decode, which should return nullif the given string was not valid JSON-encoded data ?
如果给定的字符串不是有效的 JSON 编码数据json_decode,那么 using应该返回null吗?
See example 3 on the manual page :
请参阅手册页上的示例 3:
// the following strings are valid JavaScript but not valid JSON
// the name and value must be enclosed in double quotes
// single quotes are not valid
$bad_json = "{ 'bar': 'baz' }";
json_decode($bad_json); // null
// the name must be enclosed in double quotes
$bad_json = '{ bar: "baz" }';
json_decode($bad_json); // null
// trailing commas are not allowed
$bad_json = '{ bar: "baz", }';
json_decode($bad_json); // null
回答by Kitson
Doesn't json_decode()with a json_last_error()work for you? Are you looking for just a method to say "does this look like JSON" or actually validate it? json_decode()would be the only way to effectively validate it within PHP.
不json_decode()与json_last_error()你的工作?您是否正在寻找一种方法来说明“这看起来像 JSON”还是实际验证它?json_decode()将是在 PHP 中有效验证它的唯一方法。
回答by McNally Paulo
$this->post_data = json_decode( stripslashes( $post_data ) );
if( $this->post_data === NULL )
{
die( '{"status":false,"msg":"The post_data parameter must be valid JSON"}' );
}
回答by Mohsin Hassan
This is the best and efficient way
这是最好最有效的方法
function isJson($string) {
return (json_decode($string) == null) ? false : true;
}

