将Array保存到roamingSettings

时间:2013-01-25 16:25:44

标签: javascript windows-8

有没有办法在漫游设置中保存listArrays?在js我会这样做:

var test = [];  
test.push({var1:'somevalue',var2:'someotherstuff'});

但如何将其保存在:

var appData = Windows.Storage.ApplicationData.current;
var roamingSettings = appData.roamingSettings;

当我尝试设置变量时:

roamingSettings.values['myArrayList'] = test;    

我遇到了类型错误。任何想法如何解决这个问题?

1 个答案:

答案 0 :(得分:4)

apparently无法在localSettingsroamingSettings值字段中存储对象。但是,您始终可以将数组字符串化并解析为JSON以存储它。

roamingSettings.values['myArrayList'] = JSON.stringify(test); 

test = roamingSettings.values['myArrayList'] = JSON.parse(test);

请记住数组cannot exceed 8 KB in size for roaming settings。由于您必须对其进行字符串化,因此这包括所有JSON括号和空格!