我有一个像这样的JSON字符串
$test='{"var1":null,"var3":null,"status":{"code":150,"message":"blah blah"}}';
我想访问函数中的状态码.
这是我尝试的:
$responseObj=jsonService->decode($test);//this converts the string into an Object
echo $responseObj->status->code;
现在这是行不通的.有人可以指出我正确的方向.我觉得
$responseObj->status->code
是使用错误的语法.什么是正确的语法.
我正在使用PHP 5.1.6,它没有内置的json_decode函数.所以我正在使用第三方类进行转换.我使用以下第三方课程
> @package Services_JSON
> @作者米哈尔·米格斯基
> @作者马特·纳普(Matt Knapp)
> @作者布雷特·斯蒂默曼
> @版权所有2005 Michal Migurski
> @版本CVS:$Id:JSON.php,v 1.31 2006/06/28 05:54:17 migurski Exp $
> @许可证http://www.opensource.org/licenses/bsd-license.php
> @链接http://pear.php.net/pepr/pepr-proposal-show.php?id=198
解决方法:
您应该尝试使用PHP的json_decode():
$test='{"var1":null,"var3":null,"status":{"code":150,"message":"blah blah"}}';
$responseObj = json_decode($test);
echo $responseObj->status->code;
对于PEARS的Services_JSON类(Documentation):
// create a new instance of Services_JSON
$jsonService = new Services_JSON();
$test='{"var1":null,"var3":null,"status":{"code":150,"message":"blah blah"}}';
$jsonService->decode($test);
echo $responseObj->status->code;
标签:php,json
来源: https://codeday.me/bug/20191013/1909599.html