新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
php怎么判斷數(shù)據(jù)是不是json
本文操作環(huán)境:windows7系統(tǒng)、php7.4版、DELL G3電腦

讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來自于我們對這個行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價值的長期合作伙伴,公司提供的服務(wù)項目有:域名與空間、雅安服務(wù)器托管、營銷軟件、網(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返回的是對象
判斷數(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ù)返回數(shù)據(jù)編解碼過程中發(fā)生的錯誤
注意: 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
分享標(biāo)題:php怎么判斷數(shù)據(jù)是不是json
網(wǎng)站鏈接:http://www.5511xx.com/article/cojiioe.html


咨詢
建站咨詢
