Home  >  Article  >  Backend Development  >  How to determine whether it is json data (format) in php_PHP tutorial

How to determine whether it is json data (format) in php_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:36:38950browse

First of all, remember that json_encode returns a string, and json_decode returns an object.

Judge that the data is not in JSON format:

The code is as follows
 代码如下
function is_not_json($str){  
    return is_null(json_decode($str));
}
function is_not_json($str){

return is_null(json_decode($str));
}

 代码如下
function is_json($string) { www.111cn.net
 json_decode($string);
 return (json_last_error() == JSON_ERROR_NONE);
}

Judge whether the data is legal json data: (PHP version is greater than 5.3)

The code is as follows
function is_json($string) { www.111cn.net json_decode($string);

return (json_last_error() == JSON_ERROR_NONE);

}

The json_last_error() function returns errors that occurred during the data encoding and decoding process.* 解析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;
}
 代码如下

Note: The strings operated by json encoding and decoding must be UTF8.

Example
  • The code is as follows
    * Parse json string* @param type $json_str
  • * @return type
  • */
  • function analyzeJson($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;
  • }
  • If it is not json, return false
  • ​ Articles you may be interested in
php json_encode utf-8 Chinese problem

PHP json_encode Chinese processing class instanceThe solution to the problem of missing Chinese characters using json_decode() on the GBK/GB2312 page in php Solution to the problem that json_encode cannot be called after PHP5.5 is installed php sends json data instance through curl post PHP json and array conversion support Chinese PHP’s json_encode usage analysis instructions Solution to Chinese problem in json_encode format in php
PHP JSON data processing example program usage
PHP JSON data creation and parsing program code
http://www.bkjia.com/PHPjc/738511.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/738511.htmlTechArticleFirst of all, remember that json_encode returns a string, and json_decode returns an object. To determine if the data is not in JSON format: The code is as follows function is_not_json($str){ return is_null(json_decode...
Statement:
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