简体   繁体   English

如何在Javascript中使用动态数据获取JSON数组?

[英]How to fetch JSON array with dynamic data in Javascript?

I'm currently working on university project, creating currency converter.我目前正在从事大学项目,创建货币转换器。 I would like to implement history page, where I can use JSON from exchangeratesapi.io API response and display it in a table on PHP page.我想实现历史页面,我可以在其中使用 exchangeatesapi.io API 响应中的 JSON 并将其显示在 PHP 页面的表格中。

For example: https://api.exchangeratesapi.io/history?start_at=2019-04-22&end_at=2019-04-25&base=EUR&symbols=USD例如: https : //api.exchangeratesapi.io/history?start_at=2019-04-22&end_at=2019-04-25&base=EUR&symbols=USD

{"base":"EUR","rates":{"2019-04-23":{"USD":1.1245},"2019-04-24":{"USD":1.1209},"2019-04-25":{"USD":1.1123}},"end_at":"2019-04-25","start_at":"2019-04-22"}

The problem is that the desired values are stored in an "rates" array, in which all values are changed dynamically (date and exchange rate itself) and I can not fetch and display them through the document.getElementById("").innerHTML in Javascript.问题是所需的值存储在“rates”数组中,其中所有值都是动态更改的(日期和汇率本身),我无法通过 document.getElementById("").innerHTML 中的获取和显示它们Javascript。

How can I fetch data from such an array into a table in HTML?如何将这样的数组中的数据提取到 HTML 表格中?

You can loop through data and list items as below:您可以循环遍历数据和列表项,如下所示:

for (var key in data.rates) {
    console.log('Date: '+ key);
    console.log('Rate: '+ data.rates[key]['USD']);
}

Check sample code here:在此处检查示例代码:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM