2017-06-15 48 views
0

我有下面的JSON,這是來自API的響應。鍵名在他們的名字中有點,而我使用的編碼不會識別它。在關鍵名稱中帶有一個點的PHP JSON數組

JSON:

{ 

    "unit.count": 40413, 
    "device.count": 4893, 
    "registration.count": 3951 
} 

我用來閱讀的代碼如下。

CODE:

$myArray = json_decode($response,true); 

echo $myArray['unit.count']; 

沒有回顯的

值任何幫助,將不勝感激。

羅伯特

發佈以來,我已經找到了API發送JSON腳本的問題。

感謝大家回答。

+1

從我所看到的,一切都按預期工作。你想要達到什麼目標? – Sirko

+0

在我身邊沒有顯示任何東西。只是一個空白頁 –

+0

你可以嘗試與波紋管的答案? –

回答

1

你的代碼工作正常。請確認您有有效的JSON和你正在使用正確的變量

$json = '{ 

    "unit.count": 40413, 
    "device.count": 4893, 
    "registration.count": 3951 
}'; 

$myArray = json_decode($json,true); 

echo $myArray['unit.count']; //op: 40413 

在這裏看到一個例子:https://eval.in/817059

1

試試這個

//convert json format to array is important just pass second parameter as true 
    $arr = array( 
        "unit.count" => 40413, 
        "device.count" => 4893, 
        "registration.count"=>3951 
); 

    $response= json_encode($arr); 
    $myArray = json_decode($response,true); 

    echo $myArray['unit.count']; //40413 
+0

第二個參數已經是真的檢查看問題'$ myArray = json_decode($ response,true);' –

1

請嘗試這下面的代碼。

<?php 
    $response = '{ 

"unit.count": 40413, 
"device.count": 4893, 
"registration.count": 3951 
}'; 

$myArray = json_decode($response,true); 

echo $myArray['unit.count']; 

?> 
相關問題