Javascript Money.js+;本地存储速率
我正在使用Intel XDK制作一个转换工具,我的脚本会检测设备是否有internet连接,如果是,则使用此命令通过JSON从openexchangerates获取最新速率,并将其存储在localstorage中:Javascript Money.js+;本地存储速率,javascript,html,intel-xdk,money.js,Javascript,Html,Intel Xdk,Money.js,我正在使用Intel XDK制作一个转换工具,我的脚本会检测设备是否有internet连接,如果是,则使用此命令通过JSON从openexchangerates获取最新速率,并将其存储在localstorage中: $.getJSON('https://openexchangerates.org/api/latest.json?app_id=XXXXXXXXXXXXXXXXXXXXXXXXXX',function(data) { var localData
$.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,
}
}
我真的不知道怎么做。你可以像对待普通的JS对象一样对待localStorage,所以首先你不需要对JS对象进行字符串化和解析。而不是写作
var localData = JSON.stringify(data);
localStorage.setItem('convrates', localData);
你可以这么做
localStorage['convrates'] = data;
甚至
localStorage.convrates = data;
要检索数据,您可能仍然需要使用getItem()
,因为如果convrates
不存在,这将返回null而不是抛出错误,因此要再次检索数据,只需执行以下操作即可
data = localStorage.getItem('convrates');
它将保留数据
或者null
或者最近存储在那里的convrates
对象
在localData
上调用$.getJSON
没有什么意义,因为这是一个JS对象,而不是URL。好吧,它可以:
从本地存储加载
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
}
}
});
$.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
}
}
});
保存在本地存储中
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
}
}
});
$.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
}
}
});