首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从本地windows存储文件夹读取js中的文件

从本地windows存储文件夹读取js中的文件
EN

Stack Overflow用户
提问于 2013-11-23 14:51:04
回答 1查看 1K关注 0票数 5

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

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

代码语言:javascript
运行
复制
        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中。

代码语言:javascript
运行
复制
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);
    }
});

}

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-11-26 15:44:00

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

代码语言:javascript
运行
复制
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
 });
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20163777

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档