Money.js + LocalStorage汇率

时间:2014-12-02 08:48:04

标签: javascript html5 intel-xdk money.js

我正在使用英特尔XDK制作转换工具,我的脚本会检测设备是否有互联网连接,如果是,请使用此功能通过JSON从openexchangerates获取最新费率并将其存储在localstorage中:

$.getJSON('https://openexchangerates.org/api/latest.json?app_id=XXXXXXXXXXXXXXXXXXXXXXXXXX',function(data) {
                    var localData = JSON.stringify(data);
                    localStorage.setItem('convrates', localData);
                    // Check money.js has finished loading:
                    if ( typeof fx !== "undefined" && fx.rates ) {
                        fx.rates = data.rates;
                        fx.base = data.base;
                    } else {
                        // If not, apply to fxSetup global:
                        var fxSetup = {
                            rates : data.rates,
                            base : data.base
                        }
                    }
                });

这没关系!但是当试图从localstorage获取此信息时,没有任何反应......

var localData = JSON.parse(localStorage.getItem('convrates'));
            $.getJSON(localData,function(data) {
                    // Check money.js has finished loading:
                    if ( typeof fx !== "undefined" && fx.rates ) {
                        fx.rates = data.rates;
                        fx.base = data.base;
                    } else {
                        // If not, apply to fxSetup global:
                        var fxSetup = {
                            rates : data.rates,
                            base : data.base
                        }
                    }
             });

这是JSON数据示例:

{
 "disclaimer": "Exchange rates are provided for informational purposes only, and do not constitute financial advice of any kind. Although every attempt is made to ensure quality, NO guarantees are given whatsoever of accuracy, validity, availability, or fitness for any purpose - please use at your own risk. All usage is subject to your acceptance of the Terms and Conditions of Service, available at: https://openexchangerates.org/terms/",
 "license": "Data sourced from various providers with public-facing APIs; copyright may apply; resale is prohibited; no warranties given of any kind. Bitcoin data provided by http://coindesk.com. All usage is subject to your acceptance of the License Agreement available at: https://openexchangerates.org/license/",
"timestamp": 1417507252,
"base": "USD",
"rates": {
"AED": 3.673268,
"AFN": 57.871426,
"ALL": 112.5408,
"AMD": 439.297503,
"ANG": 1.7891,
"AOA": 101.106125,
"ARS": 8.531535,
"AUD": 1.174523,
"AWG": 1.79,
"AZN": 0.783933,
"BAM": 1.570651,
"BBD": 2,
"BDT": 77.73667,
}
}

我真的不知道如何制作它。

2 个答案:

答案 0 :(得分:1)

您可以将localStorage视为普通的JS对象,因此首先您不需要对JS对象进行字符串化和解析。而不是写

var localData = JSON.stringify(data);
localStorage.setItem('convrates', localData);

你可以做到

localStorage['convrates'] = data;

甚至

localStorage.convrates = data;

要检索数据,您可能仍希望使用getItem(),因为如果convrates不存在,这将返回null而不是抛出错误,因此要再次检索数据

data = localStorage.getItem('convrates');

data null或最近存储的convrates对象保留。{/ p>

$.getJSON上调用localData并没有多大意义,因为这是一个JS对象,而不是一个URL。

答案 1 :(得分:1)

嗯,它有效:

从LocalStorage加载

var data = JSON.parse(localStorage.getItem('convrates'));

            $.each(data, function(key, value){
               // alert(key + ' = ' + value);
                    // Check money.js has finished loading:
                    if ( typeof fx !== "undefined" && fx.rates ) {
                        fx.rates = data.rates;
                        fx.base = data.base;
                    } else {
                        // If not, apply to fxSetup global:
                        var fxSetup = {
                            rates : data.rates,
                            base : data.base
                        }
                    }
             });

保存在LocalStorage中

 $.getJSON('https://openexchangerates.org/api/latest.json?app_id=3f098d62737c416c9e23ec9dc5e8b426',function(data) {
                    localStorage.setItem('convrates', JSON.stringify(data));
                    // Check money.js has finished loading:
                    if ( typeof fx !== "undefined" && fx.rates ) {
                        fx.rates = data.rates;
                        fx.base = data.base;
                    } else {
                        // If not, apply to fxSetup global:
                        var fxSetup = {
                            rates : data.rates,
                            base : data.base
                        }
                    }
                });