javascript - 从本地 Windows 存储文件夹中读取 js 中的文件

标签 javascript json windows-store-apps winjs

我的问题是我无法读取我在 C# 中创建的 json 文件。我需要我的 js 中的经度和纬度值。我需要这些来创建谷歌地图 webview。我的 js 无法找到/读取此文件。我不确定这是否是读取/制作 json 文件的正确方法。

我用这个创建了我的 JSON 文件。 beginPositie 有 2 个变量:经度和纬度。

        public async Task Serialize(Coordinate beginPositie)
        {
        string json = JsonConvert.SerializeObject(beginPositie);

        StorageFolder localFolder = ApplicationData.Current.LocalFolder;

        StorageFile MarkersFile = await localFolder.CreateFileAsync("markers.json", CreationCollisionOption.ReplaceExisting);

        using (IRandomAccessStream textStream = await MarkersFile.OpenAsync(FileAccessMode.ReadWrite))
        {
            using (DataWriter textWriter = new DataWriter(textStream))
            {
                textWriter.WriteString(json);
                await textWriter.StoreAsync();
            }
        }
    }

这是我在 JS 中读取 JSON 文件的函数。找不到“Windows”,我不知道是什么原因。我已经包含了脚本,为 js 安装了扩展 SDK,但由于某种原因我无法添加对此 SDK 的引用。

function getJSON() {
//var uri = new Windows.Foundation.Uri('ms-appdata:///local/markers.json');
//json = Windows.Storage.StorageFile.getFileFromApplicationUriAsync(uri);
$.ajax({
    url: "ms-appdata:///local/markers.json",
    success: function (data) {
        json = JSON.parse(data);
    }
});

最佳答案

查看 localFolder ApplicationData 类的属性。此代码应检索您要查找的文件数据:

Windows.Storage.ApplicationData.current.localFolder.getFileAsync("markers.json").done(
 function (file) {
    Windows.Storage.FileIO.readTextAsync(file).done(
       function (fileContent) {
          //'fileContent' contains your JSON data as a string
       },
       function (error) {
          //file couldn't be read - handle the error
       });
 },
 function (error) {
    //file not found, handle the error
 });

关于javascript - 从本地 Windows 存储文件夹中读取 js 中的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20163777/

相关文章:

javascript - jquery-如何为具有多个类的元素定义特定变量类

javascript - 无法使 document.GetElementById 正常工作

javascript - 带有模态选项的 jQuery 对话框小部件的背景/覆盖错误

javascript - 如何从 Rails 代码中删除新行 (\n)?

c# - 使用 httpClient.GetAsync 时添加 header

Windows 应用商店应用程序的日志记录框架

java - 如何在fasterxml中实现自定义序列化功能

mysql - 如何在事件日志的一个 "activity"中存储(在 MySQL 中)多个更新?

javascript - 如何使用商店插件 annotatorjs 加载注释?

localization - 如何始终在 Windows Store UWP 应用中安装所有本地化资源?