如何将我的 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 配合使用,所以这是一个很好的解决方案。感谢您给我的提示让我解决了我的问题。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您使用的是phonegap,那么您肯定可以使用文件系统。
解决方案是使用
serializeArray()
将数组编码为JSON
JQuery 中的 方法。对数组进行编码后,您将获得
JSON
字符串,您必须使用 PhoneGap 的Filewriter()
函数将其存储在文件中。有关详细信息,请访问此链接。我希望它对您有帮助:-)。
If you are using phonegap than for sure you can work with file system.
Solution is to encode your array into
JSON
usingserializeArray()
method in JQuery.Once you encode your array you will get
JSON
string which you have to store in a file using PhoneGap'sFilewriter()
function. For more detail on that visit this link.I hope it helped you :-).
Phonegap(位于 http://phonegap.com/tools/)建议 Lawnchair:http://westcoastlogic.com/lawnchair/
因此您可以将该文件读入 data.js,而不是直接将数据存储在那里
Phonegap (at http://phonegap.com/tools/) is suggesting Lawnchair: http://westcoastlogic.com/lawnchair/
so you'd read that file into data.js instead of storing the data literally there
您还可以使用 localStorage 保存数组(或者更好的是其成员),这是一种键/值存储,可以在本地存储您的数据,即使用户退出您的应用程序也是如此。查看指南Safari 开发者库。
You could also save your array (or better yet its members) using localStorage, a key/value storage that stores your data locally, even when the user quits your app. Check out the guide in the Safari Developer Library.
使用 Lawnchair 将数组保存为 JSON 对象。 JSON 对象将一直保留在内存中,直到您清除应用程序的数据。
如果您想将其永久保存到本地文件系统上的文件中,那么我想您可以编写一个phonegap插件将数据发送到插件的本机代码,该插件将创建/打开一个文件并保存它。
use Lawnchair to save the array as a JSON object. The JSON object will be there in the memory till you clear the data for the application.
If you want to save it permanently to a file on the local filesystem then i guess you can write a phonegap plugin to sent the data across to the native code of plugin which will create/open a file and save it.
JavaScript 无法直接篡改文件系统。您可以执行以下两种操作之一:
将更改(通过 AJAX)发送到 PHP 文件,该文件将在服务器上生成可下载文件,并将其提供给客户端.可能还有更多解决方案,但这是我能想到的最合理的两个。
JavaScript cannot tamper with the file system directly. You can do one of two things:
Send the changes (via AJAX) to a PHP file which would generate a downloadable file on the server, and serve it to the client.There are probably more solutions, but these are the most reasonable two I can think of.