使用php在json中检索对象

时间:2015-02-26 15:47:03

标签: php json

我正在尝试在php中检索JSON对象,但无论我尝试它只返回null。到目前为止,我已经尝试过:

$json = file_get_contents('BELOW JSON CODE');
$obj = json_decode($json,true);
var_dump($obj->live);

其中$ obj是json解码的JSON对象。

JSON对象:

{
    "live": [
    {
        "match_id": "65545",
        "has_vods": false,
        "game": "hearthstone",
        "team 1": {
        "score": "",
        "name": "World Elite HS",
        "bet": "68%"
        },
        "team 2": {
        "score": "",
        "name": "ViCi Gaming",
        "bet": "32%"
        },
        "live in": "Live",
        "title": "World Elite HS 68% vs 32% ViCi Gaming...",
        "url": "http://www.gosugamers.net/hearthstone/tournaments/5636-nel-2015-winter/1478-group-stage/5638-group-b/matches/65545-world-elite-hs-vs-vici-gaming-hearthstone",
        "tounament": "http://www.gosugamers.net/",
        "simple_title": "World Elite HS vs ViCi Gaming...",
        "streams": [
        "http://www.twitch.tv/widgets/live_embed_player.swf?channel=Curemew"
        ]
    }
    ]
}

2 个答案:

答案 0 :(得分:1)

尝试:

$json = file_get_contents('BELOW JSON CODE');
$obj = json_decode($json); //<-- decode as object and not associative array
var_dump($obj->live);

答案 1 :(得分:0)

尝试删除true函数中的json_decode选项,使其看起来像这样

$json = file_get_contents('BELOW JSON CODE');
$obj = json_decode($json);
var_dump($obj->live);