2013-05-04 125 views
0

我有這個JSON結果PHP json_decode返回NULL

[{"counter":"542"}] 
or 
    [{"counter":"542"},{"counter":"43"}] 

,我有以下代碼

$address = "http://localhost/restauranttheme/syncAndroid/getCounterGenerator4Android.php"; 
$string = file_get_contents($address); 
$json_a = json_decode($string); 

但我得到空結果$json_a == null

那麼問題是什麼?

+0

可以請你POST HTTP內容://localhost/restauranttheme/syncAndroid/getCounterGenerator4Android.php – Satya 2013-05-04 06:04:07

+0

你怎麼填充'$ json_a'。使用'print_r'或'var_dump'。 – 2013-05-04 06:04:42

+0

如何做一些錯誤檢查? url的內容是否被加載到'$ string'中? – arkascha 2013-05-04 06:06:12

回答

0

既然你已經命名了變量$ json_a我猜你期待一個數組。如果是這樣,你可能會嘗試使用一個對象作爲數組,這不會工作得很好。向json_decode添加第二個參數(true)以返回一個數組而不是一個對象。

$string = '[{"counter":"542"},{"counter":"43"}]'; 
$json_a = json_decode($string, true); 
// array(2) { [0]=> array(1) { ["counter"]=> string(3) "542" } [1]=> array(1) { ["counter"]=> string(2) "43" } } 
+0

不解釋他爲什麼變空。問題可能出現在'file_get_contents'調用中。 – xbonez 2013-05-04 07:23:26

+0

我把「真實」的參數,但它沒有工作:( 它總是返回null! – 2013-05-05 11:12:23