新聞中心
本文操作環(huán)境:windows7系統(tǒng)、php7.4版、DELL G3電腦

讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來(lái)自于我們對(duì)這個(gè)行業(yè)的熱愛(ài)。我們立志把好的技術(shù)通過(guò)有效、簡(jiǎn)單的方式提供給客戶,將通過(guò)不懈努力成為客戶在信息化領(lǐng)域值得信任、有價(jià)值的長(zhǎng)期合作伙伴,公司提供的服務(wù)項(xiàng)目有:域名與空間、雅安服務(wù)器托管、營(yíng)銷軟件、網(wǎng)站建設(shè)、吉利網(wǎng)站維護(hù)、網(wǎng)站推廣。
php怎么判斷數(shù)據(jù)是不是json?
php判斷是否為json格式的方法
http://www.poluoluo.com/jzxy/201403/265005.html
php怎么判斷數(shù)據(jù)是不是json?
首先要記住json_encode返回的是字符串, 而json_decode返回的是對(duì)象
判斷數(shù)據(jù)不是JSON格式:
代碼如下:
function is_not_json($str){
return is_null(json_decode($str));
}
判斷數(shù)據(jù)是合法的json數(shù)據(jù): (PHP版本大于5.3)
代碼如下:
function is_json($string) {
json_decode($string);
return (json_last_error() == JSON_ERROR_NONE);
}
json_last_error()函數(shù)返回?cái)?shù)據(jù)編解碼過(guò)程中發(fā)生的錯(cuò)誤
注意: json編解碼所操作字符串必須是UTF8的
例子
代碼如下:
/**
* 解析json串
* @param type $json_str
* @return type
*/
function analyJson($json_str) {
$json_str = str_replace('\\', '', $json_str);
$out_arr = array();
preg_match('/{.*}/', $json_str, $out_arr);
if (!empty($out_arr)) {
$result = json_decode($out_arr[0], TRUE);
} else {
return FALSE;
}
return $result;
}
如果不是json則返回false
網(wǎng)站欄目:php怎么判斷數(shù)據(jù)是不是json
路徑分享:http://m.fisionsoft.com.cn/article/cojiioe.html


咨詢
建站咨詢
