我正在使用Intel XDK制作一个转换工具,我的脚本检测设备是否有互联网连接,如果是的话,使用它通过JSON从开放交易所获得最新的汇率,并将其存储在本地存储中:
$.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
}
}
});这样就行了!但当试图从本地存储处获取这些信息时,什么都不会发生.
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,
}
}我真的不知道怎么做。
发布于 2014-12-03 08:16:56
嗯,它起作用了:
来自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
}
}
});https://stackoverflow.com/questions/27245235
复制相似问题