Php 为什么是这种类型的数组?
好的,我使用coinbase的API来计算BTC的当前价格,但是当我尝试使用json_decode()时,它返回了一个错误,这让我相信他们的回答不是jsonPhp 为什么是这种类型的数组?,php,arrays,json,Php,Arrays,Json,好的,我使用coinbase的API来计算BTC的当前价格,但是当我尝试使用json_decode()时,它返回了一个错误,这让我相信他们的回答不是json https://coinbase.com/api/v1/prices/spot_rate?currency=USD 这将返回: {"amount":"90.00","currency":"USD"} 我尝试了json_解码($grabPrice);$grabPrice相当于该API的一个file_get_contets()。它给我的错误
https://coinbase.com/api/v1/prices/spot_rate?currency=USD
这将返回:
{"amount":"90.00","currency":"USD"}
我尝试了json_解码($grabPrice);$grabPrice相当于该API的一个file_get_contets()。它给我的错误是:
Catchable fatal error: Object of class stdClass could not be converted to string in
如何获取PHP变量中的金额
谢谢。这是一个json编码的字符串 要从中获取数据,请首先使用 或者如果您更喜欢数组而不是对象
$data = json_decode($str, true);
echo $data["amount"];
此代码运行良好:-
$grab=file_get_contents('https://coinbase.com/api/v1/prices/spot_rate?currency=USD');
$resarray=json_decode($grab);
echo 'amount :'.$resarray->amount.'<br>';
echo 'currency :'.$resarray->currency;
什么错误?据我所知,这是JSON。我试过JSON_解码($grabPrice);$grabPrice相当于该API的一个file_get_contets()。它给我的错误是:Catchable致命错误:stdClass类的对象无法转换为string int,这是因为您试图打印返回的值,在本例中,该值是一个对象<代码>echo$returnedFromJsonDecode->amount如果您认为响应不是JSON,请在询问之前运行它。如果您不确定json_解码返回的是什么,请在询问之前将其转储。如果你收到一条错误信息,在询问之前用谷歌搜索一下。这是一个有效的演示,因为你把我揍了一顿(糟糕的笑话):阅读我的编辑/评论。
$grab=file_get_contents('https://coinbase.com/api/v1/prices/spot_rate?currency=USD');
$resarray=json_decode($grab);
echo 'amount :'.$resarray->amount.'<br>';
echo 'currency :'.$resarray->currency;
amount :89.91
currency :USD