locked
Array in localsettings RRS feed

  • Question

  • Hi, I'm making an app where I need an array of information to persist across all sessions. I assumed the way to do this was to store it in local settings, and get it out of local settings when the app launches. However, it is apparently not possible to store an array or an object in local settings:

    Windows.Storage.ApplicationData.current.localSettings.values["key1"] = "A string";                  // this line executes fine
    Windows.Storage.ApplicationData.current.localSettings.values["key2"] = ["a", "b", "c"];             // this one breaks
    Windows.Storage.ApplicationData.current.localSettings.values["key3"] = { info: "a", number: 2 };    // this one also breaks

    Is there a different way I should be doing this? I assume this is by design and not a bug

    Friday, August 31, 2012 8:33 PM

Answers

  • That's the beauty of JS

    Windows.Storage.ApplicationData.current.localSettings.values["key1"] = "A string";
    Windows.Storage.ApplicationData.current.localSettings.values["key2"] = JSON.stringify(["a", "b", "c"]);
    Windows.Storage.ApplicationData.current.localSettings.values["key3"] = JSON.stringify({ info: "a", number: 2 });
    
    // and then
    
    var value1 = Windows.Storage.ApplicationData.current.localSettings.values["key1"];
    var value2 = JSON.parse(Windows.Storage.ApplicationData.current.localSettings.values["key2"]);
    var value3 = JSON.parse(Windows.Storage.ApplicationData.current.localSettings.values["key3"]);


    • Proposed as answer by Bryan Thomas Friday, August 31, 2012 8:51 PM
    • Marked as answer by Brandon Heenan Friday, August 31, 2012 9:08 PM
    • Edited by Bryan Thomas Friday, August 31, 2012 9:10 PM
    Friday, August 31, 2012 8:51 PM

All replies

  • That's the beauty of JS

    Windows.Storage.ApplicationData.current.localSettings.values["key1"] = "A string";
    Windows.Storage.ApplicationData.current.localSettings.values["key2"] = JSON.stringify(["a", "b", "c"]);
    Windows.Storage.ApplicationData.current.localSettings.values["key3"] = JSON.stringify({ info: "a", number: 2 });
    
    // and then
    
    var value1 = Windows.Storage.ApplicationData.current.localSettings.values["key1"];
    var value2 = JSON.parse(Windows.Storage.ApplicationData.current.localSettings.values["key2"]);
    var value3 = JSON.parse(Windows.Storage.ApplicationData.current.localSettings.values["key3"]);


    • Proposed as answer by Bryan Thomas Friday, August 31, 2012 8:51 PM
    • Marked as answer by Brandon Heenan Friday, August 31, 2012 9:08 PM
    • Edited by Bryan Thomas Friday, August 31, 2012 9:10 PM
    Friday, August 31, 2012 8:51 PM
  • Okay, that was easy enough. Thanks!
    Friday, August 31, 2012 9:08 PM