2017-08-04 101 views
-2

请帮我把JSON数据 “price_usd”JSON帮助获得内容

https://api.coinmarketcap.com/v1/ticker/bitcoin-cash/

[ 
{ 
    "id": "bitcoin-cash", 
    "name": "Bitcoin Cash", 
    "symbol": "BCH", 
    "rank": "4", 
    "price_usd": "338.53", 
    "price_btc": "0.121547", 
    "24h_volume_usd": "189098000.0", 
    "market_cap_usd": "5579888431.0", 
    "available_supply": "16482700.0", 
    "total_supply": "16482700.0", 
    "percent_change_1h": "-4.29", 
    "percent_change_24h": "-23.79", 
    "percent_change_7d": "0.43", 
    "last_updated": "1501831807" 
} ] 


<?php 

$json = file_get_contents('https://api.coinmarketcap.com/v1/ticker/bitcoin-cash/'); 
$data = json_decode($json, TRUE); 

$price_usd = $data["price_usd"]; 

echo $price_usd; 

?> 

PHP脚本没有什么

+7

的[?我如何从JSON中提取数据与PHP(可能的复制https://stackoverflow.com/questions/29308898/how-do-i-extract-data-from -json-with-php) –

+0

查看数组make:print_r($ data); –

+0

WORK:echo $ data [0] ['price_usd']; –

回答

0

$数据对象的数组,所以你不能得到像数据那。 试试这个代码:

$url = "https://api.coinmarketcap.com/v1/ticker/bitcoin-cash/"; 
$dataRaw = file_get_contents($url); 
$data = json_decode($dataRaw); 
$coin = $data[0]; 
foreach ($data as $key => &$value) { 
    $value = (array)$value;//convert Object to array 
    echo $value['price_usd']; 
} 
+0

请不要回答重复的问题。 – mickmackusa