2013-07-21 31 views
-2

好吧,我使用coinbase'API来创建BTC的当前价格,但是当我尝试使用json_decode()时,它返回一个错误,导致我相信他们的响应不是JSON。为什么数组的类型是这样的?

https://coinbase.com/api/v1/prices/spot_rate?currency=USD 

返回:

{"amount":"90.00","currency":"USD"} 

我试图json_decode($ grabPrice); $ grabPrice等于该API的file_get_contets()。它给我的错误是:

Catchable fatal error: Object of class stdClass could not be converted to string in 

如何获得PHP变量中的金额?

谢谢。

+1

什么错误?据我所知,它是JSON。 –

+0

我试过json_decode($ grabPrice); $ grabPrice等于该API的file_get_contets()。它给我的错误是:可捕获的致命错误:class stdClass的对象无法转换为 – user2594145

+0

中的字符串,这是因为你试图打印返回的值,在这种情况下,它是一个对象....'echo $ returnedFromJsonDecode - >金额;'而不是 – Orangepill

回答

2

这是编码字符串一个JSON ....

来获取数据了它首先使用json_decode

$data = json_decode($str); 

echo $data->amount; 

或者如果你喜欢在数组对象

$data = json_decode($str, true); 
echo $data["amount"]; 
+0

下面是一个工作演示,因为你击败了我一拳(坏玩笑):http://codepad.org/y2sXrUXf –

+0

阅读我的编辑/评论。 – user2594145

0

这代码工作正常: -

$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