请输入您要查询的百科知识:

 

词条 json_last_error
释义

函数说明

(PHP5 >= 5.3.0)

返回最后一次以json编码数据发生的错误。

函数定义

int json_last_error ( void )

返回最后一次发生的错误,如果遇到在以json编码数据发生的错误。

参数

此函数没有参数。

返回值

返回一个integer,此值是以下一个常数:

常数 意义 有效性

JSON_ERROR_NONE 没有错误发生 

JSON_ERROR_DEPTH 超过了最大堆栈深度 

JSON_ERROR_CTRL_CHAR 发生控制特性错误,可能是有误的编码 

JSON_ERROR_STATE_MISMATCH 无效的或者是有缺陷的json 

JSON_ERROR_SYNTAX 发生语法错误 

JSON_ERROR_UTF8 有缺陷的UTF-8编码特性,也可能是有误的编码 PHP 5.3.3

实例说明

json_last_error()函数对不同integer值返回的结果。

<?php

// A valid json string

$json[] = '{"Organization": "PHP Documentation Team"}';

// An invalid json string which will cause an syntax

// error, in this case we used ' instead of " for quotation

$json[] = "{'Organization': 'PHP Documentation Team'}";

foreach($json as $string)

{

echo 'Decoding: ' . $string;

json_decode($string);

switch(json_last_error())

{

case JSON_ERROR_DEPTH:

echo ' - Maximum stack depth exceeded';

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_NONE:

echo ' - No errors';

break;

}

echo PHP_EOL;

}

?>

以上例程会输出:

Decoding: {"Organization": "PHP Documentation Team"} - No errors

Decoding: {'Organization': 'PHP Documentation Team'} - Syntax error, malformed JSON

随便看

 

百科全书收录4421916条中文百科知识,基本涵盖了大多数领域的百科知识,是一部内容开放、自由的电子版百科全书。

 

Copyright © 2004-2023 Cnenc.net All Rights Reserved
更新时间:2025/2/27 16:20:01