我正在使用 HTML5 开发 Windows 8 应用程序。该应用程序需要一些用户数据来创建新事件。该事件有标题、描述、日期和时间。我想在用户填写表单后将此数据存储在 JSON 格式的文本文件中,以便用户下次打开应用程序时可以看到它。
我可以找到将 JSON 对象写入文本文件的不同方法,但找不到与在文本文件末尾附加数据相关的任何内容。基本上,我有一个 events.txt,从中读取和显示数据,并且我想在用户添加事件后将事件附加到同一个 .txt。
我的 events.txt 看起来像这样
[
{
"key": "event1",
"title": "title1",
"description": "description1",
"date": "<sample date>",
"time": "<sample time>",
"backgroundImage": "../images/logo.png"
}
]
我正在使用以下代码从此文件中读取事件
WinJS.xhr({ url: "../data/events.txt" }).then(function (xhr) {
var events = JSON.parse(xhr.responseText);
events.forEach(function (feed) {
event.push({
group: feed,
key: feed.key,
title: feed.title,
description: feed.description,
date: feed.date,
time: feed.time,
backgroundImage: feed.backgroundImage
});
});
});
任何形式的帮助将不胜感激,因为我的截止日期很紧迫。
谢谢。
最佳答案
您在找这个 File Access sample对于消费者预览版
?
如果我正确理解你的问题,我认为这里有一些你想要使用的重要 API。
// Create a file
var eventFile;
Windows.Storage.KnownFolders.documentsLibrary.createFileAsync("event.txt", Windows.Storage.CreationCollisionOption.openIfExists).done(function (file) {
eventFile = file;
});
// Write Text to a file
Windows.Storage.FileIO.writeTextAsync(eventFile, JSON.stringify(yourJSONObject));
// Append Text to a file
Windows.Storage.FileIO.appendTextAsync(eventFile, JSON.stringify(yourJSONObject));
更多关于FileIO API的引用: http://msdn.microsoft.com/en-us/library/windows/apps/hh701440.aspx
此外,您为什么不考虑使用本地存储
或indexedDB
来存储您的事件信息?
关于json - 在 Windows 8 中将数据写入文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10561847/