json - 在 Windows 8 中将数据写入文本文件

标签 json html windows-8

我正在使用 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/

相关文章:

javascript - Django DRF 从 POST 读取 json?

xaml - 3 Pane 循环滚动面板 - 建议

c# - Windows 10 中的触摸屏友好文件选择器

html - 对齐 WordPress 主题中的图像和标题中的神秘边框

css - PNG 背景图像未在 IE 8 中显示 < 使用 html5?

node.js - Windows 64 位中的 Testaulous 无法启动浏览器

java - 无法与 jackson 合作

无法安装 Json-server,还有其他建议吗?

javascript - 在 JSON 加载到与 Angular 模块和 Controller 相同的文件中之前加载消息或微调器图标

html - 使用 CSS 在页面顶部固定一个 div