无法在localhost上检索JSON对象

时间:2014-12-27 18:02:57

标签: php json

我正在尝试在我的localhost上检索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;

为什么不输出任何内容?

1 个答案:

答案 0 :(得分: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 内部您需要echo JSON,您需要确保它有效。

<?php

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