我终于确保我的JSON对象滑过JSON验证器。但是,当我尝试解码时,它返回null。我的JSON对象如下所示:
[
{
"NAME": "Hearthstone",
"PLAYER1": "Rdu ",
"PLAYER2": "Savjz ",
"status": 2,
"meta": "LIVE"
},
{
"NAME": "LeagueofLegends",
"PLAYER1": "TeamKing",
"PLAYER2": "EDG",
"status": 2,
"meta": "28.12."
}
]
php decode:
$json = file_get_contents("crawl_JSON.php");
$json_output = json_decode($json);
var_dump($json_output);
答案 0 :(得分:2)
在您的php解码文件中,您是否使用<?
和?>
围绕它?
我文件中的更多细节:
crawl_JSON.php:
[
{
"NAME": "Hearthstone",
"PLAYER1": "Rdu ",
"PLAYER2": "Savjz ",
"status": 2,
"meta": "LIVE"
},
{
"NAME": "LeagueofLegends",
"PLAYER1": "TeamKing",
"PLAYER2": "EDG",
"status": 2,
"meta": "28.12."
}
]
test.php的:
<?
$json = file_get_contents("crawl_JSON.php");
$json_output = json_decode($json);
var_dump($json_output);
?>
我执行程序的输出:
Marks-MacBook-Pro:stackOverflow mmadej$ php -f test.php
array(2) {
[0]=>
object(stdClass)#1 (5) {
["NAME"]=>
string(11) "Hearthstone"
["PLAYER1"]=>
string(4) "Rdu "
["PLAYER2"]=>
string(6) "Savjz "
["status"]=>
int(2)
["meta"]=>
string(4) "LIVE"
}
[1]=>
object(stdClass)#2 (5) {
["NAME"]=>
string(15) "LeagueofLegends"
["PLAYER1"]=>
string(8) "TeamKing"
["PLAYER2"]=>
string(3) "EDG"
["status"]=>
int(2)
["meta"]=>
string(6) "28.12."
}
}
答案 1 :(得分:2)
如果您将PHP脚本作为普通文件访问,则不会运行它们,您必须通过Web服务器访问它们。所以改成它:
$json = file_get_contents("http://localhost/crawl_JSON.php");
假设脚本位于文档根目录中,您可能需要添加从文档根目录到脚本的完整路径。