添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Authentication Services
Command Line Specific Extensions
Compression and Archive Extensions
Cryptography Extensions
Database Extensions
Date and Time Related Extensions
File System Related Extensions
Human Language and Character Encoding Support
Image Processing and Generation
Mail Related Extensions
Mathematical Extensions
Non-Text MIME Output
Process Control Extensions
Other Basic Extensions
Other Services
Search Engine Extensions
Server Specific Extensions
Session Extensions
Text Processing
Variable and Type Related Extensions
Web Services
Windows Only Extensions
XML Manipulation
GUI Extensions
Keyboard Shortcuts
?
This help
Next menu item
Previous menu item
Previous man page
Next man page
Scroll to bottom
Scroll to top
Goto homepage
Goto search
(current page)
Focus search box
返回给定的 string 是否是语法上有效的 JSON。 如果 json_validate() 返回 true json_decode() 在使用相同的 depth flags 时,将成功解码给定的字符串。 如果 json_validate() 返回 false ,原因可以使用 json_last_error() json_last_error_msg() 获取。 如果解码后的 JSON 内容未被使用, json_validate() 相比于 json_decode() 使用了较少的内存,因为它不需要构建包含有效载荷的数组或对象结构。 在 json_decode() 之前调用 json_validate() 将不必要地解析字符串两次, 因为 json_decode() 在解码过程中隐式地执行验证。 json_validate() 只有在解码后的 JSON 内容不会立即使用,但需要知道字符串是否包含有效的 JSON 时才有用。

示例 #1 json_validate() 示例

<?php
var_dump
( json_validate ( '{ "test": { "foo": "bar" } }' ));
var_dump ( json_validate ( '{ "": "": "" } }' ));
?>

以上示例会输出:

bool(true)
bool(false)
Behrad
1 year ago
---------------- PHP < 8.3 ----------------

function json_validate(string $string): bool {
json_decode($string);

return json_last_error() === JSON_ERROR_NONE;
}

var_dump(json_validate('{ "test": { "foo": "bar" } }')); // true

---------------- PHP >= 8.3 ----------------

var_dump(json_validate('{ "test": { "foo": "bar" } }')); // true

Note: code from https://www.php.net/releases/8.3/en.php
Julien T.
1 year ago
Building upon Allan R.'s initial idea, I've developed an improved version of the json_validate function for those using PHP 8.2 and earlier versions. This function emulates the functionality introduced in PHP 8.3, providing an effective way to validate JSON strings in earlier PHP versions.

```php
if (!function_exists('json_validate')) {
/**
* Validates a JSON string.
*
* @param string $json The JSON string to validate.
* @param int $depth Maximum depth. Must be greater than zero.
* @param int $flags Bitmask of JSON decode options.
* @return bool Returns true if the string is a valid JSON, otherwise false.
*/
function json_validate($json, $depth = 512, $flags = 0) {
if (!is_string($json)) {
return false;
}

try {
json_decode($json, false, $depth, $flags | JSON_THROW_ON_ERROR);
return true;
} catch (\JsonException $e) {
return false;
}
}
}
```

Key Improvements:

- String Check: Added a validation to ensure the input is a string.
- Error Handling: Utilizes try-catch to effectively catch and handle JsonException.
- Backward Compatibility: Safely integrable in older PHP versions, automatically deferring to native functionality in PHP 8.3+.