2013-05-04 160 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