简体   繁体   English

PHP json_decode()无法正常工作

[英]PHP json_decode() not working

I have a problem I am getting a json file I can echo it and it looks like this 我有一个问题,我正在获取一个json文件,可以回显它,它看起来像这样

[{"sys_data":"0MxPPaza","date":"2015-02-15","objective":"VIDEO"}]

in my code I am doing this 在我的代码中,我正在这样做

$json = FROM THE SERVER;
$obj = json_decode($json);
$res = $obj->["objective"];
echo $res;

res is NULL obj is NULL also res是NULL obj也是NULL

Your json_decode call returns an array, with one member. 您的json_decode调用返回一个包含一个成员的数组。

Here is a dump of your json object: 这是您的json对象的转储:

array (size=1) 0 => object(stdClass)[10] public 'sys_data' => string '0MxPPaza' (length=8) public 'date' => string '2015-02-15' (length=10) public 'objective' => string 'VIDEO' (length=5)

so replace this line: 因此,请替换此行:

$res = $obj->["objective"];

With this: 有了这个:

$res = $obj[0]->objective;

Just replace [] quotes to {} . 只需将[]引号替换为{} Like $res = $obj[0]->{"objective"}; $res = $obj[0]->{"objective"};

Or you may use assoc array convertation instead of object: 或者您可以使用assoc数组转换代替object:

$json = FROM THE SERVER;
$obj = json_decode($json, true);
$res = $obj["objective"];
echo $res;`

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

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