php访问对象的成员,如何使用PHP从JSON访问另一个对象内的类的成员

我有一个像这样的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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值