【发布时间】:2018-02-16 09:51:26
【问题描述】:
我正在调用以下 API url:https://api.gemini.com/v1/trades/btcusd?timestamp=1518710400&limit_trades=1
这将返回以下 JSON:
[
{
"timestamp":1518710409,
"timestampms":1518710409004,
"tid":3051346543,
"price":"9837.17",
"amount":"0.00118501",
"exchange":"gemini",
"type":"sell"
}
]
当我尝试像这样从 json 字符串访问“价格”对象时:
$response = $client->request('GET', 'https://api.gemini.com/v1/trades/btcusd?timestamp=1518710400&limit_trades=1');
$body = json_decode($response->getBody());
var_dump() 返回:
array(1) { [0]=> object(stdClass)#85 (7) { ["timestamp"]=> int(1518710409) ["timestampms"]=> float(1518710409004) ["tid"]=> float(3051346543) ["price"]=> string(7) "9837.17" ["amount"]=> string(10) "0.00118501" ["exchange"]=> string(6) "gemini" ["type"]=> string(4) "sell" } }
但我收到以下错误:
Notice: Trying to get property of non-object in (file path) on line 130
第 130 行正在
echo $body->price
为什么$body->price 不是从 JSON 字符串返回的“价格”的有效访问器?
【问题讨论】:
-
您的 JSON 响应是一个对象数组。