【问题标题】:Using openexchangerates API for currency conversion using JS/JQuery使用 openexchangerates API 使用 JS/JQuery 进行货币转换
【发布时间】:2015-01-27 21:39:57
【问题描述】:

我已经设法使用 JS 将 JSON 数据放入表单控件中:

$("#getRates").ready(function () {
    $.getJSON("http://openexchangerates.org/api/currencies.json?app_id="APP_ID", function (data) {
        console.log(data);

        for (var value in data) {
        if (data.hasOwnProperty(value)) {
            var text = document.createTextNode(value);
        var select = document.getElementsByClassName('form-control')[1];

        select.appendChild(document.createElement('option')).appendChild(text);


    }
}

当前的 JSON 文件仅包含国家/地区名称,但我希望使用名为 Latest.JSON 并包含最新汇率的不同 JSON 文件,但它具有以下字段:

“许可证”: “时间戳”:1417258840, “基础”:“美元”, “费率”:{ “AED”:3.672743, “AFN”:57.800375,

我怎样才能将“费率”附加到表单功能并使用费率进行转换?

正如我之前尝试过的那样,我只是 console.log 并收到“License”“timestamp”“base”但没有费率?

没有具体的答案,也许只是一些寻找方向的方向?

【问题讨论】:

    标签: javascript jquery html json api


    【解决方案1】:

    $.getJSON("http://openexchangerates.org/api/latest.json?app_id=b65b6f0a06204a6087bab9a63a5845b7", 函数(数据){ console.log(data.rates);

        for (var key in data.rates) {
        if (data.rates.hasOwnProperty(key)) {
            var text = document.createTextNode(key);
        var select = document.getElementsByClassName('form-control')[1];
    
    
    
        console.log(select);
    
        select.appendChild(document.createElement('option')).appendChild(text);
    
    
    }
    }
    
       for (var value in data.rates) {
        if (data.rates.hasOwnProperty(value)) {
            var text = document.createTextNode(value);
        var select = document.getElementsByClassName('form-control')[2];
    
    
     console.log(select);
    
        select.appendChild(document.createElement('option')).appendChild(text);
    
    
        }
    }
    
    });
    });
    

    我将 var 键更改为 data.rates,这似乎解决了它。

    现在这会使用 latest.json 中的 data.rates 值填充我的两个表单控件 ..

    console.log() 仅供我自己使用..

    【讨论】:

      猜你喜欢
      • 2011-03-17
      • 2017-11-20
      • 1970-01-01
      • 1970-01-01
      • 2014-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-29
      相关资源
      最近更新 更多