仅从JSON数据

时间:2018-01-25 00:35:57

标签: javascript json ajax

编辑 - 将问题标记为重复的目的是什么?赢得一些积分并得到一点智慧的踢?你无法知道这个问题对某些人没有帮助。这个"重复的问题的答案"在我有限的知识中不会回答我的问题,但回答我的问题的绅士做了。 -edit

我希望从JSON数据列表中只选择一个值,即24小时价格百分比变化:

{
    "id": "stellar", 
    "name": "Stellar", 
    "symbol": "XLM", 
    "rank": "6", 
    "price_usd": "0.570132", 
    "price_btc": "0.00005009", 
    "24h_volume_usd": "672209000.0", 
    "market_cap_usd": "10187093680.0", 
    "available_supply": "17867956333.0", 
    "total_supply": "103629819514", 
    "max_supply": null, 
    "percent_change_1h": "1.8", 
    "percent_change_24h": "16.65", 
    "percent_change_7d": "23.95", 
    "last_updated": "1516839244"
} 

目前,我目前的代码只是为了测试我到目前为止的工作情况,只返回[object Object]



<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
	$(document).ready(function () {
		$.getJSON('https://api.coinmarketcap.com/v1/ticker/stellar/',
		function (data) {
			document.body.append(data);
		});
	});
</script>
&#13;
&#13;
&#13;

我希望孤立 - 并开始,只是为了展示 - 只有"percent_change_24h"并从那里开始工作。

谢谢。

2 个答案:

答案 0 :(得分:2)

好吧,您可以直接使用该密钥percent_change_24h

进行访问

var data = {
  "id": "stellar",
  "name": "Stellar",
  "symbol": "XLM",
  "rank": "6",
  "price_usd": "0.570132",
  "price_btc": "0.00005009",
  "24h_volume_usd": "672209000.0",
  "market_cap_usd": "10187093680.0",
  "available_supply": "17867956333.0",
  "total_supply": "103629819514",
  "max_supply": null,
  "percent_change_1h": "1.8",
  "percent_change_24h": "16.65",
  "percent_change_7d": "23.95",
  "last_updated": "1516839244"
};

console.log(data['percent_change_24h']);
document.body.append(data['percent_change_24h']);
// in your case document.body.append(data['percent_change_24h']);

希望有所帮助!

答案 1 :(得分:1)

https://api.coinmarketcap.com/v1/ticker/stellar/返回数组:

[
    {
        "id": "stellar", 
        "name": "Stellar", 
        "symbol": "XLM", 
        "rank": "6", 
        "price_usd": "0.566242", 
        "price_btc": "0.00004991", 
        "24h_volume_usd": "674523000.0", 
        "market_cap_usd": "10117586651.0", 
        "available_supply": "17867955133.0", 
        "total_supply": "103629819514", 
        "max_supply": null, 
        "percent_change_1h": "-0.26", 
        "percent_change_24h": "16.45", 
        "percent_change_7d": "21.53", 
        "last_updated": "1516840744"
    }
]

因此,要访问percent_change_24h字段,您需要data[0].percent_change_24h

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
    <script>
        $(document).ready(function () {
                $.getJSON('https://api.coinmarketcap.com/v1/ticker/stellar/',
                function (data) {
                    document.body.append(data[0].percent_change_24h);
                });
        });
    </script>