2014-12-27 37 views
1

我想检索我的本地主机上的JSON对象,问题是它不会输出任何东西。 JSON对象可能如下所示:不能检索本地主机上的JSON对象

[ 
{ 
    NAME: "Hearthstone", 
    PLAYER1: "Rdu ", 
    PLAYER2: "Savjz ", 
    status: 2, 
    meta: "LIVE" 
}, 
{ 
    NAME: "League of Legends", 
    PLAYER1: "Team King ", 
    PLAYER2: "EDG ", 
    status: 2, 
    meta: "28.12." 
}] 

php检索对象。

$url = "http://localhost:8888/crawl_JSON.php"; 
$json = file_get_contents($url); 
$json_output = json_decode($json); 


echo $json_output; 

为什么不输出任何东西?

+0

你确定该链接返回一个json对象吗? – gbestard 2014-12-27 18:05:25

+0

而不是使用PHP下载,使用浏览器。这是否足够有效,即返回200状态码和预期的JSON数据?如果没有,那么你看错了地方。 – 2014-12-27 18:09:03

回答

0

请注意,json_decode()返回一个对象,您不能echo您将需要使用var_dumpprint_r。如果你想回显,你可以回显JSON字符串。

$url = "http://localhost:8888/crawl_JSON.php"; 
$json = file_get_contents($url); 
echo $json; 
$json_output = json_decode($json); 

var_dump($json_output); 

crawl_JSON.php里面你需要echoJSON,你将需要确保它是有效的。

<?php 

echo ' 
[ 
    { 
     "NAME": "Hearthstone", 
     "PLAYER1": "Rdu ", 
     "PLAYER2": "Savjz ", 
     "status": 2, 
     "meta": "LIVE" 
    }, 
    { 
     "NAME": "LeagueofLegends", 
     "PLAYER1": "TeamKing", 
     "PLAYER2": "EDG", 
     "status": 2, 
     "meta": "28.12." 
    } 
] 
'; 
+0

谁downvote this – meda 2014-12-27 18:07:02

+1

什么是重要的是为什么不是谁 – gbestard 2014-12-27 18:09:38

+0

它为什么现在返回null? – 2014-12-27 18:14:02