从本地Windows应用商店文件夹中读取js中的文件

时间:2013-11-23 14:51:05

标签: 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);
    }
});

}

1 个答案:

答案 0 :(得分:2)

查看ApplicationData课程的localFolder属性。此代码应检索您正在查找的文件数据:

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
 });