我试图获得price_usd,但我尝试使用此
data["price_usd"]
并没有帮助,所以我想如何获取数据?
json table:
[
{
"id": "bitcoin-cash",
"name": "Bitcoin Cash",
"symbol": "BCH",
"rank": "3",
"price_usd": "2957.36",
"price_btc": "0.211189",
"24h_volume_usd": "1339490000.0",
"market_cap_usd": "49908111624.0",
"available_supply": "16875900.0",
"total_supply": "16875900.0",
"max_supply": "21000000.0",
"percent_change_1h": "-0.09",
"percent_change_24h": "2.4",
"percent_change_7d": "34.95",
"last_updated": "1514247253"
}
]
答案 0 :(得分:0)
首先需要将json文本转换为php数组/对象。
使用此代码作为示例:
<?php
$rawJSon="your long json here"
$data=json_decode($rawJson, true);
echo $data[0]["price_usd"];
答案 1 :(得分:0)
假设这是你的JSON字符串:
$raw = <<<EOT
[
{
"id": "bitcoin-cash",
"name": "Bitcoin Cash",
"symbol": "BCH",
"rank": "3",
"price_usd": "2957.36",
"price_btc": "0.211189",
"24h_volume_usd": "1339490000.0",
"market_cap_usd": "49908111624.0",
"available_supply": "16875900.0",
"total_supply": "16875900.0",
"max_supply": "21000000.0",
"percent_change_1h": "-0.09",
"percent_change_24h": "2.4",
"percent_change_7d": "34.95",
"last_updated": "1514247253"
}
]
EOT;
由于你有一个JSON对象,你需要使用箭头指定一个对象属性(注意你的JSON中的对象花括号):
$data = json_decode( $raw );
echo $data[0]-> price_usd;
或者您可以解码为数组而不是对象:
$data = json_decode( $raw, TRUE );
echo $data[0]['price_usd'];