json_decode - 返回 NULL
json_decode - returning NULL
下面的 JSON 字符串被传递到 json_decode()
,它返回 NULL
。
{"total_goals":26,"total_games":17,"average_goals":"1.53"}
这是我的代码:
$homeJSON = file_get_contents("http://strategy-bets.com/archive/archive.php?baseurl=http://www.totalcorner.com".$Home_Team_Link);
$homeJSON = str_replace("\xEF\xBB\xBF",'',$homeJSON);
$homeJSON = rtrim($homeJSON);
$homeJSON = html_entity_decode($homeJSON);
$homeJSON = preg_replace('/\s+/', '', $homeJSON);
$clean = rtrim($homeJSON, "\x00..\x1F");
$home_decoded = json_decode($clean);
$home_decoded
仍然是 NULL
。
首先是json_decode()
is returning NULL
due to an error condition. You can determine the nature of the error condition using json_last_error()
,其中returns一个整数常量,表示错误的类型。您可以使用此函数解码这些(根据 json_last_error()
手册页中描述的错误代码构建):
<?php
function decodeJsonError($errorCode)
{
$errors = array(
JSON_ERROR_NONE => 'No error has occurred',
JSON_ERROR_DEPTH => 'The maximum stack depth has been exceeded',
JSON_ERROR_STATE_MISMATCH => 'Invalid or malformed JSON',
JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded',
JSON_ERROR_SYNTAX => 'Syntax error',
JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded',
JSON_ERROR_RECURSION => 'One or more recursive references in the value to be encoded',
JSON_ERROR_INF_OR_NAN => 'One or more NAN or INF values in the value to be encoded',
JSON_ERROR_UNSUPPORTED_TYPE => 'A value of a type that cannot be encoded was given'
);
if (isset($errors[$errorCode]))
{
return $errors[$errorCode];
}
return 'Unknown error';
}
现在,我无法弄清楚您正在调用的 API 返回了什么,因为没有提供有效的 link(我知道这可能是敏感数据) ,但是,让我们尝试一种不同的方法,尝试从 API 返回的任何内容中仅提取 JSON:
<?php
$homeJSON = file_get_contents("http://strategy-bets.com/archive/archive.php?baseurl=http://www.totalcorner.com".$Home_Team_Link);
if (!preg_match('/(\{"[^\}]+\})/', $homeJSON, $matches))
{
echo "An error occurred; no JSON found.";
return;
}
$home_decoded = json_decode($matches[1]);
对我来说,这与 API(没有适当的 URL)的实际输出相反:
file_get_contents("http://strategy-bets.com/archive/archive.php?baseurl=http://www.totalcorner.com");
即使 API 本身返回 HTML 与 JSON 混合在一起。
编辑:因为你提供了一个有效的URL,我做了更多的调试:
php > print_r(array_map('dechex', array_map('ord', str_split($homeJSON))));
Array
(
[0] => ef
[1] => bb
[2] => bf
[3] => 7b
[4] => 22
[5] => 74
[6] => 6f
[7] => 74
[8] => 61
[9] => 6c
...
字节 7b
是有效 JSON 开始的开头 {
。字节 0-3
是一个 UTF-8 Byte Order Mark。因此,虽然我上面的正则表达式解决方案仍然有效,但我们也可以像这样清理 BOM 和任何零散的空字符:
$homeJSON = file_get_contents("http://strategy-bets.com/archive/archive.php?baseurl=http://www.totalcorner.com".$Home_Team_Link);
$homeJSON = trim($homeJSON, "\x0\xEF\xBB\xBF");
$home_decoded = json_decode($homeJSON);
这给了我:
php > var_dump(json_decode($jsonCopy));
object(stdClass)#1 (3) {
["total_goals"]=>
int(26)
["total_games"]=>
int(17)
["average_goals"]=>
string(4) "1.53"
}
下面的 JSON 字符串被传递到 json_decode()
,它返回 NULL
。
{"total_goals":26,"total_games":17,"average_goals":"1.53"}
这是我的代码:
$homeJSON = file_get_contents("http://strategy-bets.com/archive/archive.php?baseurl=http://www.totalcorner.com".$Home_Team_Link);
$homeJSON = str_replace("\xEF\xBB\xBF",'',$homeJSON);
$homeJSON = rtrim($homeJSON);
$homeJSON = html_entity_decode($homeJSON);
$homeJSON = preg_replace('/\s+/', '', $homeJSON);
$clean = rtrim($homeJSON, "\x00..\x1F");
$home_decoded = json_decode($clean);
$home_decoded
仍然是 NULL
。
首先是json_decode()
is returning NULL
due to an error condition. You can determine the nature of the error condition using json_last_error()
,其中returns一个整数常量,表示错误的类型。您可以使用此函数解码这些(根据 json_last_error()
手册页中描述的错误代码构建):
<?php
function decodeJsonError($errorCode)
{
$errors = array(
JSON_ERROR_NONE => 'No error has occurred',
JSON_ERROR_DEPTH => 'The maximum stack depth has been exceeded',
JSON_ERROR_STATE_MISMATCH => 'Invalid or malformed JSON',
JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded',
JSON_ERROR_SYNTAX => 'Syntax error',
JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded',
JSON_ERROR_RECURSION => 'One or more recursive references in the value to be encoded',
JSON_ERROR_INF_OR_NAN => 'One or more NAN or INF values in the value to be encoded',
JSON_ERROR_UNSUPPORTED_TYPE => 'A value of a type that cannot be encoded was given'
);
if (isset($errors[$errorCode]))
{
return $errors[$errorCode];
}
return 'Unknown error';
}
现在,我无法弄清楚您正在调用的 API 返回了什么,因为没有提供有效的 link(我知道这可能是敏感数据) ,但是,让我们尝试一种不同的方法,尝试从 API 返回的任何内容中仅提取 JSON:
<?php
$homeJSON = file_get_contents("http://strategy-bets.com/archive/archive.php?baseurl=http://www.totalcorner.com".$Home_Team_Link);
if (!preg_match('/(\{"[^\}]+\})/', $homeJSON, $matches))
{
echo "An error occurred; no JSON found.";
return;
}
$home_decoded = json_decode($matches[1]);
对我来说,这与 API(没有适当的 URL)的实际输出相反:
file_get_contents("http://strategy-bets.com/archive/archive.php?baseurl=http://www.totalcorner.com");
即使 API 本身返回 HTML 与 JSON 混合在一起。
编辑:因为你提供了一个有效的URL,我做了更多的调试:
php > print_r(array_map('dechex', array_map('ord', str_split($homeJSON))));
Array
(
[0] => ef
[1] => bb
[2] => bf
[3] => 7b
[4] => 22
[5] => 74
[6] => 6f
[7] => 74
[8] => 61
[9] => 6c
...
字节 7b
是有效 JSON 开始的开头 {
。字节 0-3
是一个 UTF-8 Byte Order Mark。因此,虽然我上面的正则表达式解决方案仍然有效,但我们也可以像这样清理 BOM 和任何零散的空字符:
$homeJSON = file_get_contents("http://strategy-bets.com/archive/archive.php?baseurl=http://www.totalcorner.com".$Home_Team_Link);
$homeJSON = trim($homeJSON, "\x0\xEF\xBB\xBF");
$home_decoded = json_decode($homeJSON);
这给了我:
php > var_dump(json_decode($jsonCopy));
object(stdClass)#1 (3) {
["total_goals"]=>
int(26)
["total_games"]=>
int(17)
["average_goals"]=>
string(4) "1.53"
}