如何将我的 javascript 数组保存到文件中?

发布于 2024-12-24 19:00:00 字数 498 浏览 3 评论 0原文

我的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦里南柯 2024-12-31 19:00:00

如果您使用的是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 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 :-).

肩上的翅膀 2024-12-31 19:00:00

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

夏尔 2024-12-31 19:00:00

您还可以使用 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.

梦途 2024-12-31 19:00:00

使用 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.

清旖 2024-12-31 19:00:00

JavaScript 无法直接篡改文件系统。您可以执行以下两种操作之一:

  1. 将更改保存到 cookie 并在下次读取它
  2. 将更改(通过 AJAX)发送到 PHP 文件,该文件将在服务器上生成可下载文件,并将其提供给客户端.

可能还有更多解决方案,但这是我能想到的最合理的两个。

JavaScript cannot tamper with the file system directly. You can do one of two things:

  1. Save the changes onto a cookie and read it the next time
  2. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文