©
本文檔使用 php中文網手册 發布
(PHP 5 >= 5.5.0, PHP 7)
json_last_error_msg — Returns the error string of the last json_encode() or json_decode() call
此函数没有参数。
Returns the error message on success, "No Error" if no
error has occurred, 或者在失败时返回 FALSE
.
[#1] Anonymous [2015-06-03 09:34:14]
Here's an updated version of the function:
<?php
if (!function_exists('json_last_error_msg')) {
function json_last_error_msg() {
static $ERRORS = array(
JSON_ERROR_NONE => 'No error',
JSON_ERROR_DEPTH => 'Maximum stack depth exceeded',
JSON_ERROR_STATE_MISMATCH => 'State mismatch (invalid or malformed JSON)',
JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded',
JSON_ERROR_SYNTAX => 'Syntax error',
JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded'
);
$error = json_last_error();
return isset($ERRORS[$error]) ? $ERRORS[$error] : 'Unknown error';
}
}
?>
[#2] qrworld.net [2014-11-07 10:18:21]
I found a full article about json_last_error and json_last_error_msg that shows the result depending on the fact that exists or not the functions due to the PHP version.
In this url you have this full article: http://softontherocks.blogspot.com/2014/11/funcion-jsonlasterror-y.html
[#3] Anonymous [2014-10-15 08:37:50]
seregynp :
a better way to test JSON
<?php
$json = json_decode('wrong json');
if ($json === null && json_last_error() !== JSON_ERROR_NONE) {
throw new \LogicException(sprintf("Failed to parse json string '%s', error: '%s'", $json , json_last_error_msg()));
}
?>
[#4] seregynp at gmail dot com [2014-04-13 13:26:55]
Watch out !
In case no error occurred, 'No error' still returned !
So when upgrading to php 5.5, unexpected behavior can be:
<?php
if ($error = json_last_error_msg()) {
throw new \LogicException(sprintf("Failed to parse json string '%s', error: '%s'", $this->data, $error));
}
// in php 5.5 exception will be thrown, while as in older version not
?>
[#5] Anonymous [2013-09-17 10:39:25]
if (!function_exists('json_last_error_msg')) {
function json_last_error_msg() {
static $errors = array(
JSON_ERROR_NONE => null,
JSON_ERROR_DEPTH => 'Maximum stack depth exceeded',
JSON_ERROR_STATE_MISMATCH => 'Underflow or the modes mismatch',
JSON_ERROR_CTRL_CHAR => 'Unexpected control character found',
JSON_ERROR_SYNTAX => 'Syntax error, malformed JSON',
JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded'
);
$error = json_last_error();
return array_key_exists($error, $errors) ? $errors[$error] : "Unknown error ({$error})";
}
}
[#6] Ess [2013-07-22 20:19:31]
If function not exists
<?php
if (!function_exists('json_last_error_msg'))
function json_last_error_msg()
{
switch (json_last_error()) {
default:
return;
case JSON_ERROR_DEPTH:
$error = 'Maximum stack depth exceeded';
break;
case JSON_ERROR_STATE_MISMATCH:
$error = 'Underflow or the modes mismatch';
break;
case JSON_ERROR_CTRL_CHAR:
$error = 'Unexpected control character found';
break;
case JSON_ERROR_SYNTAX:
$error = 'Syntax error, malformed JSON';
break;
case JSON_ERROR_UTF8:
$error = 'Malformed UTF-8 characters, possibly incorrectly encoded';
break;
}
throw new Exception($error);
}
?>