Tools, FAQ, Tutorials:
json_last_error_msg() - PHP JSON Error Message
How to detect errors occurred in the json_decode() call?
✍: FYIcenter.com
You can use the following two functions to detect error and display error message
occurred during the last call of json_decode() or json_encode().
Here is a PHP example that shows you how to detect and display errors of a json_decode() call:
<?php
# json_decode_error.php
# Copyright (c) FYIcenter.com
$json = '{"site":"dev.fyicenter.com","topics":{"PHP":"Y","JSON":"Y"]}';
print("\nInput: ".$json."\n");
# Decoded into an array
$array = json_decode($json,true);
if (json_last_error() == JSON_ERROR_NONE) {
print("\nOutput Array:\n");
print(" Type: ".gettype($array)."\n");
print(" Size: ".count($array)."\n");
print(" ['site']: ".$array["site"]."\n");
print(" ['topics']['JSON']: ".$array["topics"]["JSON"]."\n");
print("\nOutput Array Dump:\n");
var_dump($array);
} else {
print("\njson_decode() error: ".json_last_error_msg()."\n");
}
?>
If you run the above PHP code through the PHP engine, you get the following output:
>\fyicenter\php\php.exe json_decode_error.php
Input: {"site":"dev.fyicenter.com","topics":{"PHP":"Y","JSON":"Y"]}
json_decode() error: State mismatch (invalid or malformed JSON)
Unfortunately, the error message is not detailed enough to tell us where the error occurred in the JSON text string.
⇒ json_encode() Function in PHP
2018-03-04, ∼4625🔥, 0💬
Popular Posts:
Can Two Forms Be Nested? Can two forms be nested? The answer is no and yes: No. You can not nest two...
How To Remove Slashes on Submitted Input Values in PHP? By default, when input values are submitted ...
What's Wrong with "while ($c=fgetc($f)) {}" in PHP? If you are using "while ($c=fgetc($f)) {}" to lo...
How to use "{{...}}" Liquid Codes in "set-body" Policy Statement? The "{{...}}" Liquid Codes in "set...
How to use the "Ctrl-p Ctrl-q" sequence to detach console from the TTY terminal of container's runni...