如何将我的JavaScript数组保存到文件? [英] How to save my javascript array to a file?

查看:283
本文介绍了如何将我的JavaScript数组保存到文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的phonegap/iOS应用程序有一个名为 data.js 的文件,其中包含一个数组.该应用程序与此数组配合使用,并将一些更改存储到其中.

现在,当用户退出我的应用程序时,我希望它将更改保存到 data.js 文件中.有什么办法吗?

更新:这是我的数组( data.js 中唯一的内容):

    var data = [
        [0, "A", "B", "0", "0"],
        [1, "C", "D", "0", "0"],
        [2, "E", "F", "0", "0"],
        ...
    ];

已解决! 我使用JSON对数组进行字符串化并将其保存到localStorage.它仅需与Mobile Safari一起使用,因此这是一个很好的解决方案.感谢您给我一些提示,让我能够解决问题.

解决方案

如果您正在使用phonegap,则可以使用文件系统. 解决方案是使用JQuery中的 serializeArray() 方法将数组编码为JSON.

对数组进行编码后,您将获得JSON字符串,必须使用PhoneGap的Filewriter()函数将其存储在文件中.有关该内容的详细信息,请访问此链接.

我希望它对您有所帮助:-).

My phonegap/iOS application has a file named data.js which contains an array. The application works with this array and some changes are stored into it.

Now when the user quits my application I want it to save the changes to the data.js file. Is there any way to do this?

UPDATE This is my array (the only thing in data.js):

    var data = [
        [0, "A", "B", "0", "0"],
        [1, "C", "D", "0", "0"],
        [2, "E", "F", "0", "0"],
        ...
    ];

SOLVED! I used JSON to stringify my array and save it to the localStorage. It only has to work with Mobile Safari, so this is a good solution. Thanks for giving me the hints that made me solve my problem.

解决方案

If you are using phonegap than for sure you can work with file system. Solution is to encode your array into JSON using serializeArray() method in JQuery.

Once you encode your array you will get JSON string which you have to store in a file using PhoneGap's Filewriter() function. For more detail on that visit this link.

I hope it helped you :-).

这篇关于如何将我的JavaScript数组保存到文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆