php中对内置函数json_encode和json_decode的异常处理

2022-07-22,,,,

在php中,json_encode和json_decode是很常用的函数,具体用法可以查看相关文档,这里主要说一下错误处理。

平时我们在使用这两个方法的时候可能没怎么注意错误处理,有时候如果传入的参数格式不正确就会导致报错了。下面是错误处理的方式:

json_last_error — 返回最后发生的错误,如果有,返回 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 指定的类型,值无法编码。 php 5.5.0
json_error_invalid_property_name 指定的属性名无法编码。 php 7.0.0
json_error_utf16 畸形的 utf-16 字符,可能因为字符编码不正确。 php 7.0.0

php官方参考文档:https://secure.php.net/manual/zh/function.json-last-error.php

到此这篇关于php中对内置函数json_encode和json_decode的异常处理的文章就介绍到这了,更多相关php处理json_encode和json_decode异常内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

《php中对内置函数json_encode和json_decode的异常处理.doc》

下载本文的Word格式文档,以方便收藏与打印。