How does php determine whether the data is json?
I hope you can read it carefully and be able to achieve something!
Php method to determine whether the data is json: 1, create a PHP example file; 2, through the "function analyJson ($json_str) {...}" method to determine whether the data is json.
This article operating environment: windows7 system, PHP7.4 version, DELL G3 computer
How does php tell if the data is json?
The method of judging whether it is in json format by php
Http://www.poluoluo.com/jzxy/201403/265005.html
How does php tell if the data is json?
The first thing to remember is that json_encode returns a string and json_decode returns an object
Determine that the data is not in JSON format:
The code is as follows:
Function is_not_json ($str) {return is_null (json_decode ($str));}
Determine that the data is legitimate json data: (PHP version is greater than 5.3)
The code is as follows:
Function is_json ($string) {json_decode ($string); return (json_last_error () = = JSON_ERROR_NONE);}
The json_last_error () function returns errors that occur during data encoding and decoding.
Note: the string operated by json codec must be UTF8.
Examples
The code is as follows:
Return false if it is not json
This is the end of the content of "how php determines whether the data is json". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!