2017-01-05 99 views
-1

我試圖讓這個變量$data舉行的令牌 - 這是我通過PHP - 解碼JSON

獲得
$data = json_decode($result, true); 

JSON:

{ 
    "response": { 
     "status": "OK", 
     "token": "goog:11229:116ewfd1c0f5c:goog", 
     "dbg_info": { 
      "instance": "53.google.com", 
      "slave_hit": false, 
      "db": "master", 
      "parent_dbg_info": { 
       "instance": "23.google.com", 
       "slave_hit": false, 
       "db": "master", 
       "parent_dbg_info": { 
        "instance": "44.google.com", 
        "slave_hit": false, 
        "db": "master", 
        "time": 212.100215784, 
        "version": "1.19", 
        "warnings": [], 
        "slave_lag": 0, 
        "start_microtime": 123620305.0342 
       } 

我已經解碼的JSON和它在$召開數據

我試圖讓

echo $data[response]->[token]; 

echo $data[response][token]; 
+0

顯示解碼json的代碼 –

+0

顯示將json解碼爲$ data的代碼行。它很重要。 – YvesLeBorg

+0

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

回答

3

鍵應該在引號中。試試這樣:

echo $data['response']['token']; 

假設你的字符串,如:

  $json='{"response":{"status":"OK","token":"goo:187089:586e5e5cc0666:goo","dbg_info":{"instance":"50.bm-goo.prod.ams1","slave_hit":false,"db":"master","parent_dbg_info":{"instance":"71.goo.prod.nym2","slave_hit":false,"db":"master","parent_dbg_info":{"instance":"44.goo.nym2","slave_hit":false,"db":"master","time":735.19611358643,"version":"1.17","warnings":[],"slave_lag":1,"start_microtime":1483628123.6313},"awesomesauce_cache_used":false,"count_cache_used":false,"uuid":"306aa2c0-f625-55c8-8984-db43b434b289","warnings":[],"time":1451.1139392853,"start_microtime":1483628123.557,"version":"1.17.150","slave_lag":0,"output_term":"not_found"},"awesomesauce_cache_used":false,"count_cache_used":false,"uuid":"48060f64-9b7c-50fd-a243-b17d3064a9a7","warnings":[],"time":1635.9009742737,"start_microtime":1483628123.4259,"version":"1.17.150","slave_lag":1,"output_term":"not_found","master_instance":"71.goo.prod.nym2","proxy":true,"master_time":1451.1139392853}}} '; 

$data=json_decode($json,true); 
echo $data['response']['token'];//goog:11229:116ewfd1c0f5c:goog 

DEMO

+0

酷 - 當我這樣做時,我輸出整個json,而不僅僅是令牌。 –

+0

@RodrigoBoratto你可以在這裏查看json。 http://json.parser.online.fr/。在答案中添加的json是正確的。複製粘貼整個代碼並在本地運行以查看輸出。 –

+1

@RodrigoBoratto然後你應該在問題區域發表評論。 –

-1

如果您正在使用JSON_DECODE(VAR,真)解碼,其結果將是一個關聯數組(因爲在第二個參數中爲true),所以你必須這樣做:

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

Full Exa mple:

$json = ' 
{ 
    "response":{ 
     "status":"OK", 
     "token":"goog:11229:116ewfd1c0f5c:goog", 
     "dbg_info":{ 
     "instance":"53.google.com", 
     "slave_hit":false, 
     "db":"master", 
     "parent_dbg_info":{ 
      "instance":"23.google.com", 
      "slave_hit":false, 
      "db":"master", 
      "parent_dbg_info":{ 
       "instance":"44.google.com", 
       "slave_hit":false, 
       "db":"master", 
       "time":212.100215784, 
       "version":"1.19", 
       "warnings":[ 

       ], 
       "slave_lag":0, 
       "start_microtime":123620305.0342 
      } 
     } 
     } 
    } 
} 
'; 
$data = json_decode($json, true); 

echo $data["response"]["token"]; 
+0

OP說,否則,解碼爲一個關聯數組。 – YvesLeBorg

+0

ops!然後改變聯想。 –