is_json() 函数本身不是 PHP 的内置函数。但是,您可以使用 json_decode() 和 json_last_error() 函数来检查一个字符串是否为有效的 JSON 格式,包括嵌套的 JSON。
以下是一个示例函数 is_json(),用于检查字符串是否为有效的 JSON:
function is_json($string) { json_decode($string); return (json_last_error() == JSON_ERROR_NONE);}使用此函数,您可以检查嵌套的 JSON 字符串是否有效。例如:
$json = '{"key": {"nestedKey": "value"}}';if (is_json($json)) { echo "The string is valid JSON.";} else { echo "The string is not valid JSON.";}这将输出 “The string is valid JSON.”,因为 $json 是一个有效的嵌套 JSON 字符串。


