我的问题是我无法读取我在 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/