(PHP 5 >= 5.3.0, PHP 7)
json_last_error — ����������Ĵ���
����У����� JSON �������ʱ������Ĵ���
�˺���û�в�����
����һ�����ͣ�integer�������ֵ�������µij���֮һ��
���� | ���� | ������ |
---|---|---|
JSON_ERROR_NONE |
û�д����� | |
JSON_ERROR_DEPTH |
����������ջ��� | |
JSON_ERROR_STATE_MISMATCH |
��Ч���쳣�� JSON | |
JSON_ERROR_CTRL_CHAR |
�����ַ��������DZ��벻�� | |
JSON_ERROR_SYNTAX |
����� | |
JSON_ERROR_UTF8 |
�쳣�� UTF-8 �ַ���Ҳ������Ϊ����ȷ�ı��롣 | PHP 5.3.3 |
JSON_ERROR_RECURSION |
One or more recursive references in the value to be encoded | PHP 5.5.0 |
JSON_ERROR_INF_OR_NAN |
One or more
NAN
or INF
values in the value to be encoded
|
PHP 5.5.0 |
JSON_ERROR_UNSUPPORTED_TYPE |
ָ�������ͣ�ֵ�����롣 | PHP 5.5.0 |
JSON_ERROR_INVALID_PROPERTY_NAME |
ָ���������������롣 | PHP 7.0.0 |
JSON_ERROR_UTF16 |
���ε� UTF-16 �ַ���������Ϊ�ַ����벻��ȷ�� | PHP 7.0.0 |
Example #1 json_last_error() ����
<?php
// һ����Ч�� json �ַ���
$json[] = '{"Organization": "PHP Documentation Team"}';
// һ����Ч�� json �ַ����ᵼ��һ����������������������ʹ�� ' ������ " ��Ϊ����
$json[] = "{'Organization': 'PHP Documentation Team'}";
foreach ($json as $string) {
echo 'Decoding: ' . $string;
json_decode($string);
switch (json_last_error()) {
case JSON_ERROR_NONE:
echo ' - No errors';
break;
case JSON_ERROR_DEPTH:
echo ' - Maximum stack depth exceeded';
break;
case JSON_ERROR_STATE_MISMATCH:
echo ' - Underflow or the modes mismatch';
break;
case JSON_ERROR_CTRL_CHAR:
echo ' - Unexpected control character found';
break;
case JSON_ERROR_SYNTAX:
echo ' - Syntax error, malformed JSON';
break;
case JSON_ERROR_UTF8:
echo ' - Malformed UTF-8 characters, possibly incorrectly encoded';
break;
default:
echo ' - Unknown error';
break;
}
echo PHP_EOL;
}
?>
�������̻������
Decoding: {"Organization": "PHP Documentation Team"} - No errors Decoding: {'Organization': 'PHP Documentation Team'} - Syntax error, malformed JSON
Example #2 json_encode() �� json_last_error()
<?php
// ���� UTF8 ����
$text = "\xB1\x31";
$json = json_encode($text);
$error = json_last_error();
var_dump($json, $error === JSON_ERROR_UTF8);
?>
�������̻������
string(4) "null" bool(true)