ตอบคำถาม
ฟังก์ชันjson_last_errorส่งคืนข้อผิดพลาดล่าสุดที่เกิดขึ้นระหว่างการเข้ารหัสและถอดรหัส JSON ดังนั้นวิธีที่เร็วที่สุดในการตรวจสอบ JSON ที่ถูกต้องคือ
// decode the JSON data
// set second parameter boolean TRUE for associative array output.
$result = json_decode($json);
if (json_last_error() === JSON_ERROR_NONE) {
    // JSON is valid
}
// OR this is equivalent
if (json_last_error() === 0) {
    // JSON is valid
}
โปรดทราบว่าjson_last_errorได้รับการสนับสนุนใน PHP> = 5.3.0 เท่านั้น
  โปรแกรมเต็มรูปแบบเพื่อตรวจสอบข้อผิดพลาดที่แน่นอน
เป็นการดีที่จะทราบข้อผิดพลาดที่แน่นอนในระหว่างการพัฒนา นี่คือโปรแกรมเต็มรูปแบบเพื่อตรวจสอบข้อผิดพลาดที่แน่นอนขึ้นอยู่กับเอกสาร PHP
function json_validate($string)
{
    // decode the JSON data
    $result = json_decode($string);
    // switch and check possible JSON errors
    switch (json_last_error()) {
        case JSON_ERROR_NONE:
            $error = ''; // JSON is valid // No error has occurred
            break;
        case JSON_ERROR_DEPTH:
            $error = 'The maximum stack depth has been exceeded.';
            break;
        case JSON_ERROR_STATE_MISMATCH:
            $error = 'Invalid or malformed JSON.';
            break;
        case JSON_ERROR_CTRL_CHAR:
            $error = 'Control character error, possibly incorrectly encoded.';
            break;
        case JSON_ERROR_SYNTAX:
            $error = 'Syntax error, malformed JSON.';
            break;
        // PHP >= 5.3.3
        case JSON_ERROR_UTF8:
            $error = 'Malformed UTF-8 characters, possibly incorrectly encoded.';
            break;
        // PHP >= 5.5.0
        case JSON_ERROR_RECURSION:
            $error = 'One or more recursive references in the value to be encoded.';
            break;
        // PHP >= 5.5.0
        case JSON_ERROR_INF_OR_NAN:
            $error = 'One or more NAN or INF values in the value to be encoded.';
            break;
        case JSON_ERROR_UNSUPPORTED_TYPE:
            $error = 'A value of a type that cannot be encoded was given.';
            break;
        default:
            $error = 'Unknown JSON error occured.';
            break;
    }
    if ($error !== '') {
        // throw the Exception or exit // or whatever :)
        exit($error);
    }
    // everything is OK
    return $result;
}
  การทดสอบด้วย JSON INPUT ที่ถูกต้อง
$json = '[{"user_id":13,"username":"stack"},{"user_id":14,"username":"over"}]';
$output = json_validate($json);
print_r($output);
  ผลลัพธ์ที่ถูกต้อง
Array
(
    [0] => stdClass Object
        (
            [user_id] => 13
            [username] => stack
        )
    [1] => stdClass Object
        (
            [user_id] => 14
            [username] => over
        )
)
  การทดสอบด้วย JSON ที่ไม่ถูกต้อง
$json = '{background-color:yellow;color:#000;padding:10px;width:650px;}';
$output = json_validate($json);
print_r($output);
  OUTPUT ไม่ถูกต้อง
Syntax error, malformed JSON.
  หมายเหตุพิเศษสำหรับ (PHP> = 5.2 && PHP <5.3.0)
เนื่องจากjson_last_errorไม่รองรับใน PHP 5.2 คุณสามารถตรวจสอบว่าการเข้ารหัสหรือถอดรหัสคืนค่าบูลีนFALSEหรือไม่ นี่คือตัวอย่าง
// decode the JSON data
$result = json_decode($json);
if ($result === FALSE) {
    // JSON is invalid
}
หวังว่านี่จะเป็นประโยชน์ Happy Coding!
               
              
json_decodeครั้งเดียว ...json_decodeนอกจากนี้ยังตรวจสอบการป้อนข้อมูลและการกลับมาของค่า