无法访问或解析PHP中解码的json结构


Unable to access or parse decoded json structure in PHP

我正在尝试访问解码的json结构,其中$encoded包含来自shopify API GET /admin/orders/450789469.json的响应(请参阅他们的文档)。

$decoded= json_decode($encoded_input, true);
var_dump($decoded);

$decoded的转储显示已解码的嵌套数组,但当我尝试访问各个元素时,什么都不会显示。

echo $decoded->orders[0]->buyer_accepts_marketing; 

有人能解释一下为什么解码后的json结构无法访问吗?感谢

当使用带有"true"作为第二个参数的json_decode()时,所有内容都会变成数组而不是对象。

尝试$decoded['orders'][0]['buyer_accepts_marketing']

$decoded= json_decode($encoded_input, true);

告诉PHP将字符串解码为数组,但是在行

$decoded->orders[0]->buyer_accepts_marketing; 

您尝试将其作为对象进行访问。你可以尝试使用

$decoded['orders'][0]['buyer_accepts_marketing'];

相反。

编辑:另请参阅文档:http://www.php.net/manual/en/function.json-decode.php

编辑2:根据api规范,您应该访问

$decoded['order']['buyer_accepts_marketing'];