简体   繁体   English

PHP json_decode使用json数组进行查询时返回null

[英]PHP json_decode return null when delling with json array

i have this json result 我有这个json结果

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

and i have the following code 我有以下代码

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

but i am getting null result $json_a == null 但我得到空结果$json_a == null

So what is the problem? 那是什么问题呢?

Since you have named the variable $json_a I guess you're expecting an array. 由于您已将变量命名为$ json_a,所以我猜您正在期待一个数组。 If so you may be trying to use an object as an array, which wont work too well. 如果是这样,您可能正在尝试将一个对象用作数组,这将不能很好地工作。 Add a second param (true) to json_decode to return an array instead of an object. 向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" } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM