2

我正在尝试访问解码的 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 结构无法访问?谢谢

4

2 回答 2

5

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

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

于 2013-06-26T15:13:48.227 回答
2

线

$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'];
于 2013-06-26T15:14:32.063 回答