Home > php教程 > php手册 > body text

PHP: 判断是否是JSON数据

WBOY
Release: 2016-06-06 19:50:57
Original
1388 people have browsed it

首先要记住json_encode返回的是字符串, 而json_decode返回的是对象. 判断数据不是JSON格式: function is_not_json ( $str ){ return is_null(json_decode ( $str )); } 判断数据是合法的json数据: (PHP版本大于5.3) function is_json ( $string ) { json_dec

首先要记住json_encode返回的是字符串, 而json_decode返回的是对象.

判断数据不是JSON格式:

<code><span>function</span><span> is_not_json</span><span>(</span><span>$str</span><span>){</span><span>return</span><span> is_null(json_decode</span><span>(</span><span>$str</span><span>));</span><span>}</span></code>
Copy after login


判断数据是合法的json数据: (PHP版本大于5.3)

<code><span>function</span><span> is_json</span><span>(</span><span>$string</span><span>)</span><span>{</span><span>
 json_decode</span><span>(</span><span>$string</span><span>);</span><span>return</span><span>(</span><span>json_last_error</span><span>()</span><span>==</span><span> JSON_ERROR_NONE</span><span>);</span><span>}
</span></code>
Copy after login
json_last_error()函数返回数据编解码过程中发生的错误.

注意: json编解码所操作字符串必须是UTF8的.


by iefreer


参考:

http://www.php.net/manual/en/ref.json.php

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template