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/

相关文章:

android - 如何在 ListView 中显示不同的 json 字符串?

javascript - 将 JSON 数组引号替换为方括号 [

html - 如何阻止容器 div 允许内容溢出?

.net - 在 Windows 8 Metro 风格的 c# 应用程序上捕获 Enter 和空格键

php - Xcode 7 beta swift 使用未解析的标识符

node.js - Webpack/babel 意外 token ,预期为 ";"

jquery - 使用 jQuery 的随机图像幻灯片

html - 为什么在 IE 和 Opera 中我的 div 之间没有显示页面背景色?

windows - Windows 8 应用程序开发是否有简写名称?

c# - Windows 8 Consumer Preview + Visual Studio 11 Developer Preview 中的 Socket BUG