如何显示JSON API中的特定信息?
如何显示JSON API数组中存储的特定变量的值 例如,我如何使用coinmarketcaps JSON API()在特定wordpress帖子中以美元显示当前比特币价格 API提供了以下输出:如何显示JSON API中的特定信息?,json,ajax,wordpress,Json,Ajax,Wordpress,如何显示JSON API数组中存储的特定变量的值 例如,我如何使用coinmarketcaps JSON API()在特定wordpress帖子中以美元显示当前比特币价格 API提供了以下输出: [ { "id": "bitcoin", "name": "Bitcoin", "symbol": "BTC", "rank": "1", "price_usd": "3351.98", "price_btc": "1.0", "24h
[
{
"id": "bitcoin",
"name": "Bitcoin",
"symbol": "BTC",
"rank": "1",
"price_usd": "3351.98",
"price_btc": "1.0",
"24h_volume_usd": "1455740000.0",
"market_cap_usd": "55289274334.0",
"available_supply": "16494512.0",
"total_supply": "16494512.0",
"percent_change_1h": "0.55",
"percent_change_24h": "3.45",
"percent_change_7d": "17.52",
"last_updated": "1502145551"
}
]
我只需要显示“price_usd”tho的值
我尝试过这样做,但没有成功:
<script>
var btcPrice;
function UpdateBtcPrice(){
$.ajax({
type: "GET",
url: "https://api.coinmarketcap.com/v1/ticker/bitcoin/",
dataType: "json",
success: function(result){
btcPrice = result[0].price_usd;
},
error: function(err){
console.log(err);
}
});
}
</script>
var-btcPrice;
函数UpdateBtcPrice(){
$.ajax({
键入:“获取”,
url:“https://api.coinmarketcap.com/v1/ticker/bitcoin/",
数据类型:“json”,
成功:功能(结果){
btcPrice=结果[0]。价格(美元);
},
错误:函数(err){
控制台日志(err);
}
});
}
任何帮助都将不胜感激 您需要调用函数来执行请求: var btcPrice; function UpdateBtcPrice(){ $.ajax({ type: "GET", url: "https://api.coinmarketcap.com/v1/ticker/bitcoin/", dataType: "json", success: function(result){ btcPrice = result[0].price_usd; }, error: function(err){ console.log(err); } }); } UpdateBtcPrice(); var-btcPrice; 函数UpdateBtcPrice(){ $.ajax({ 键入:“获取”, url:“https://api.coinmarketcap.com/v1/ticker/bitcoin/", 数据类型:“json”, 成功:功能(结果){ btcPrice=结果[0]。价格(美元); }, 错误:函数(err){ 控制台日志(err); } }); } UpdateBtcPrice();
您需要调用函数来执行请求: var btcPrice; function UpdateBtcPrice(){ $.ajax({ type: "GET", url: "https://api.coinmarketcap.com/v1/ticker/bitcoin/", dataType: "json", success: function(result){ btcPrice = result[0].price_usd; }, error: function(err){ console.log(err); } }); } UpdateBtcPrice(); var-btcPrice; 函数UpdateBtcPrice(){ $.ajax({ 键入:“获取”, url:“https://api.coinmarketcap.com/v1/ticker/bitcoin/", 数据类型:“json”, 成功:功能(结果){ btcPrice=结果[0]。价格(美元); }, 错误:函数(err){ 控制台日志(err); } }); } UpdateBtcPrice(); 你可以试试这个代码
<?php
//get data with api call
$response = file_get_contents('https://api.coinmarketcap.com/v1/ticker/bitcoin/');
$response = json_decode($response);
echo $response[0]->price_usd;//print the value
?>
您可以尝试此代码
<?php
//get data with api call
$response = file_get_contents('https://api.coinmarketcap.com/v1/ticker/bitcoin/');
$response = json_decode($response);
echo $response[0]->price_usd;//print the value
?>
我知道这一点。尽管如此,我还是不能让它工作。我想我必须将这个函数添加到我当前wordpress主题的functions.php文件中,然后可以在任何需要的地方请求这个函数。不幸的是,wordpress在我为它的函数添加新函数时就坏了。php@Voxcon-你不能在functions.php中使用javascript或jQuery,你必须把这个函数放在模板文件中,从client-browser.ah我知道了。在js文件夹中将函数保存为UpdateBtcPrice.js,现在我可以通过php函数调用访问它了!谢谢我知道。尽管如此,我还是不能让它工作。我想我必须将这个函数添加到我当前wordpress主题的functions.php文件中,然后可以在任何需要的地方请求这个函数。不幸的是,wordpress在我为它的函数添加新函数时就坏了。php@Voxcon-你不能在functions.php中使用javascript或jQuery,你必须把这个函数放在模板文件中,从client-browser.ah我知道了。在js文件夹中将函数保存为UpdateBtcPrice.js,现在我可以通过php函数调用访问它了!谢谢苏维克·西达尔:非常感谢!你的方法简单多了,效果很好!苏维克·西达尔:非常感谢!你的方法简单多了,效果很好!