2014-12-02 69 views
1

我正在使用英特爾XDK製作轉換工具,我的腳本檢測設備是否具有互聯網連接,如果是使用此功能從openexchangerates通過JSON獲取最新費率並將其存儲在localstorage中:Money.js + 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, 
} 
} 

我真的不知道如何做到這一點。

回答

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 
         } 
        } 
       }); 
1

您可以像對待普通的JS對象那樣對待localStorage類型,所以首先您不需要對JS對象進行字符串化和解析。而不是寫

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

的你可以做

localStorage['convrates'] = data; 

甚至

localStorage.convrates = data; 

檢索數據,你可能仍然要使用getItem(),因爲這將返回null,而不是如果convrates不存在,則拋出一個錯誤,因此要重新檢索數據,您只需執行

data = localStorage.getItem('convrates'); 

這將離開data或者null或最近存儲在那裏的convrates對象。

調用$.getJSONlocalData沒有任何意義,因爲這是一個JS對象,而不是URL。