欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

php判斷是否為json格式的方法

 更新時(shí)間:2014年03月04日 14:44:20   投稿:zxhpj  
這篇文章主要介紹了php判斷是否為json格式的方法,需要的朋友可以參考下

首先要記住json_encode返回的是字符串, 而json_decode返回的是對(duì)象

判斷數(shù)據(jù)不是JSON格式:

復(fù)制代碼 代碼如下:

function is_not_json($str){ 
    return is_null(json_decode($str));
}

判斷數(shù)據(jù)是合法的json數(shù)據(jù): (PHP版本大于5.3)

復(fù)制代碼 代碼如下:

function is_json($string) { www.dbjr.com.cn
 json_decode($string);
 return (json_last_error() == JSON_ERROR_NONE);
}

json_last_error()函數(shù)返回?cái)?shù)據(jù)編解碼過程中發(fā)生的錯(cuò)誤

注意: json編解碼所操作字符串必須是UTF8的

例子

復(fù)制代碼 代碼如下:

/**
* 解析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

PS:關(guān)于json操作,這里再為大家推薦幾款比較實(shí)用的json在線工具供大家參考使用:

在線JSON代碼檢驗(yàn)、檢驗(yàn)、美化、格式化工具:
http://tools.jb51.net/code/json

JSON在線格式化工具:
http://tools.jb51.net/code/jsonformat

在線XML/JSON互相轉(zhuǎn)換工具:
http://tools.jb51.net/code/xmljson

json代碼在線格式化/美化/壓縮/編輯/轉(zhuǎn)換工具:
http://tools.jb51.net/code/jsoncodeformat

在線json壓縮/轉(zhuǎn)義工具:

http://tools.jb51.net/code/json_yasuo_trans

C語言風(fēng)格/HTML/CSS/json代碼格式化美化工具:
http://tools.jb51.net/code/ccode_html_css_json

相關(guān)文章

最新評(píng)論