位置: 编程技术 - 正文

PHP:json_last_error()的用法_JSON函数

编辑:rootadmin
json_last_error

推荐整理分享PHP:json_last_error()的用法_JSON函数,希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:,内容如对您有帮助,希望把文章链接给更多的朋友!

(PHP 5 >= 5.3.0)

json_last_error — 返回最后发生的错误

说明 int json_last_error ( void )

如果有,返回 JSON 编码解码时最后发生的错误。

参数

此函数没有参数。

返回值

返回一个整型(integer),这个值会是以下的常量之一:

JSON 错误码 常量 含义 可用性 JSON_ERROR_NONE 没有错误发生 JSON_ERROR_DEPTH 到达了最大堆栈深度 JSON_ERROR_STATE_MISMATCH 无效或异常的 JSON JSON_ERROR_CTRL_CHAR 控制字符错误,可能是编码不对 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 A value of a type that cannot be encoded was given PHP 5.5.0 范例 PHP:json_last_error()的用法_JSON函数

Example #1 json_last_error() 例子

<?php//一个有效的json字符串$json[]='{"Organization":"PHPDocumentationTeam"}';//一个无效的json字符串会导致一个语法错误,在这个例子里我们使用'代替了"作为引号$json[]="{'Organization':'PHPDocumentationTeam'}";foreach($jsonas$string){echo'Decoding:'.$string;json_decode($string);switch(json_last_error()){caseJSON_ERROR_NONE:echo'-Noerrors';break;caseJSON_ERROR_DEPTH:echo'-Maximumstackdepthexceeded';break;caseJSON_ERROR_STATE_MISMATCH:echo'-Underfloworthemodesmismatch';break;caseJSON_ERROR_CTRL_CHAR:echo'-Unexpectedcontrolcharacterfound';break;caseJSON_ERROR_SYNTAX:echo'-Syntaxerror,malformedJSON';break;caseJSON_ERROR_UTF8:echo'-MalformedUTF-8characters,possiblyincorrectlyencoded';break;default:echo'-Unknownerror';break;}echoPHP_EOL;}?>

以上例程会输出:

Example #2 json_encode() 的 json_last_error()

<?php//无效的UTF8序列$text="xB1x";$json=json_encode($text);$error=json_last_error();var_dump($json,$error===JSON_ERROR_UTF8);?>

以上例程会输出:

参见

json_last_error_msg() - Returns the error string of the last json_encode() or json_decode() call json_decode() - 对 JSON 格式的字符串进行编码 json_encode() - 对变量进行 JSON 编码

PHP:json_last_error_msg()的用法_JSON函数 json_last_error_msg(PHP5=5.5.0)json_last_error_msgReturnstheerrorstringofthelastjson_encode()orjson_decode()call说明stringjson_last_error_msg(void)参数此函数没有参数。返回值Returnstheer

PHP:system()的用法_命令行函数 system(PHP4,PHP5)system执行外部程序,并且显示输出说明stringsystem(string$command[,int&$return_var])同C版本的system()函数一样,本函数执行command参数所指定的命令,

PHP:proc_open()的用法_命令行函数 proc_open(PHP4=4.3.0,PHP5)proc_open执行一个命令,并且打开用来输入/输出的文件指针。说明resourceproc_open(string$cmd,array$descriptorspec,array&$pipes[,string$cwd[,array$env[,ar

本文链接地址:https://www.jiuchutong.com/biancheng/284698.html 转载请保留说明!

上一篇:PHP:json_decode()的用法_JSON函数(php json)

下一篇:PHP:json_last_error_msg()的用法_JSON函数

免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

鄂ICP备2023003026号

网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

友情链接: 武汉网站建设 电脑维修 湖南楚通运网络