京东API Json数据如何转为PHP数组调用?

新手上路,请多包涵

不知为何,京东API接口数据不能用foreach调用,接口数据返回的是stdClass类。用json_decode返回NULL,无法转为数组。json_last_error_msg无错误代码。请问我该如何用PHP调用这个json中的单个数据?如:goodsName。先谢过了!

$c = new JdClient();
$c->appKey = "xxxxxxx";
$c->appSecret = "xxxxxxx";
$c->accessToken = "xxxxxxxxxxx";
$c->serverUrl = "https://api.jd.com/routerjson";
$req = new UnionOpenGoodsPromotiongoodsinfoQueryRequest();
$skuIds="25055951175";
$req->setSkuIds($skuIds);
$req->setVersion("1.0");
$resp = $c->execute($req, $c->accessToken);
//$data= json_decode($resp, true);
//var_dump($resp);
//echo json_last_error_msg();

?>
<?php
foreach($resp as $item)
{ foreach($item as $key)
{
?>    
<?php }} ?>
<?php echo $key->goodsName; ?>

返回的Json数据:

object(stdClass)#10 (1) {
  ["jd_union_open_goods_promotiongoodsinfo_query_responce"]=>
  object(stdClass)#9 (2) {
    ["code"]=>
    string(1) "0"
    ["queryResult"]=>
    string(742) "{"code":200,"data":[{"unitPrice":309.00,"materialUrl":"http://item.jd.com/25055951175.html","endDate":1666713600000,"isFreeFreightRisk":1,"isFreeShipping":1,"commisionRatioWl":1.0,"commisionRatioPc":1.0,"imgUrl":"http://img14.360buyimg.com/n1/jfs/t1/110207/30/32379/181285/63516e97Ecf33cfa2/2c2b3238ac4a4c19.jpg","vid":30871,"cidName":"食品饮料","wlUnitPrice":309.00,"cid2Name":"休闲食品","isSeckill":0,"cid2":1583,"cid3Name":"坚果炒货","inOrderCount":100,"cid3":1591,"shopId":29859,"isJdSale":0,"goodsName":"三只松鼠坚果大礼包1583g坚果礼盒节日礼物送礼礼品干果核桃零食 1583g","skuId":25055951175,"startDate":1666627200000,"cid":1320}],"message":"接口成功","requestId":"o_0b72e2f0_l9nrahon_43240294"}"
  }
}
回复
阅读 606
1 个回答

看起来你用的应该是这个包

在初始化那里设置一下对象的 $returnObject 属性为 false 就好了。

$c = new JdClient();
$c->appKey = "xxxxxxx";
+ JdClient::$returnObject = false;
...

也可以用通用的方法:get_object_vars($resp) 或者 json_decode(json_encode($resp), true)

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏