JSON解析 - 输出NULL - PHP

时间:2017-01-05 15:29:49

标签: php json

我正在查询输出json的第三方服务。

$data = json_decode($result,true);
var_dump ($data);

$ data认为:

{"response":{"status":"OK","token":"hbapi:187089:586e655ed1f9c:nym2","dbg_info":{"instance":"53.bm-hbapi.prod.ams1","slave_hit":false,"db":"master","parent_dbg_info":{"instance":"64.bm-hbapi.prod.nym2","slave_hit":false,"db":"master","parent_dbg_info":{"instance":"45.bm-api.prod.nym2","slave_hit":false,"db":"master","time":664.07299041748,"version":"1.17","warnings":[],"slave_lag":0,"start_microtime":1483629917.8571},"awesomesauce_cache_used":false,"count_cache_used":false,"uuid":"39e1c17a-7fe4-58ac-9486-c4dd5bbf96a3","warnings":[],"time":1159.7349643707,"start_microtime":1483629917.7835,"version":"1.17.150","slave_lag":0,"output_term":"not_found"},"awesomesauce_cache_used":false,"count_cache_used":false,"uuid":"286ca4bc-6964-50ad-b241-ff9df1304278","warnings":[],"time":1347.2578525543,"start_microtime":1483629917.6534,"version":"1.17.150","slave_lag":0,"output_term":"not_found","master_instance":"64.bm-hbapi.prod.nym2","proxy":true,"master_time":1159.7349643707}}}

我正在尝试获取令牌值。

我试过

$token = $data["response"][0]["token"];

我得到了空白

我也试过

$token = $data['response'][0]['token'];

我仍然得到NULL。

我看过How can I access an array/object?和其他线索 - 无法找到问题。

5 个答案:

答案 0 :(得分:1)

$token = $data["response"]["token"];

JSON:

{
    "response": {
        "status": "OK",
        "token": "hbapi:187089:586e655ed1f9c:nym2",
        "dbg_info": {

        }
    }
}

答案 1 :(得分:0)

$data是一个对象(不是数组),因此您可以访问response作为此对象的属性:$data->response

response又是一个对象。等。

例如status可以这样调用:$data->response->status

您可以使用以下代码查看什么是对象以及什么是数组:

print "<pre>";
var_dump($data);

所以在你的情况下,要获得token

$token = $data->response->token

答案 2 :(得分:0)

如果不是数组,为什么要访问pos [0]?

而不是尝试访问所需的参数,如下所示:

$token = $data["response"]["token"];

答案 3 :(得分:0)

$data['response'][0]['token'];更改为$data['response']['token'];

$ data ['response']是,它没有0索引。

{
    "status": "OK",
    "token": "hbapi:187089:586e655ed1f9c:nym2",
    "dbg_info": {

}

答案 4 :(得分:-1)

在$ data中你仍然有json,所以你应该再次调用json_decode($ data)。