如何在 NativeScript 中访问 JSON 文件?
How to access JSON file in NativeScript?
你好,我正在尝试访问一个 JSON 文件到我的 nativescrpt 项目。我试过了
let fs = require("tns-core-modules/file-system");
let documents = fs.knownFolders.currentApp();
function GetJsonData(callback) {
let filePath = documents.getFile("./shared/message.json");
let array;
let jsonData;
jsonFile.readText().then(function (content) {
try {
jsonData = JSON.parse(content);
array = new observableArrayModule.ObservableArray(jsonData);
} catch (err) {
throw new Error('Could not parse JSON file');
}
}, function (error) {
throw new Error('Could not read JSON file');
});
function showJsonData() {
GetJsonData((array) => {
console.log(array);
});
}
但是无法从 JSON 文件
中获取数据
您应该首先获取文件夹,并且您应该使用 knownFolders
起点执行此操作。
import { knownFolders } from 'tns-core-modules/file-system';
knownFolders.currentApp().getFolder('shared').getFile('message.json').readText ...;
如果这个文件在你的源项目中 - 你必须确保它被 webpack 保存。
在您的 webpack.config.js 文件中
new CopyWebpackPlugin([
{ from: { glob: "**/*.jpg" } },
{ from: { glob: "**/shared/*.json" } },
{ from: { glob: "**/*.png" } }
首先,您必须检查该文件是否包含在项目的 webpack.config.js 文件中。就像@dashman 说的。
在你的 webpack 中找到这个
new CopyWebpackPlugin([
{ from: { glob: "fonts/**" } },
{ from: { glob: "**/*.jpg" } },
{ from: { glob: "**/*.png" } },
], { ignore: [`${relative(appPath, appResourcesFullPath)}/**`] }),
然后改成这样
new CopyWebpackPlugin([
{ from: { glob: "fonts/**" } },
{ from: { glob: "**/*.jpg" } },
{ from: { glob: "**/shared/*.json" } },
{ from: { glob: "**/*.png" } },
], { ignore: [`${relative(appPath, appResourcesFullPath)}/**`] }),
之后你的代码也有很多错误。
改成这样
let fs = require("tns-core-modules/file-system");
let documents = fs.knownFolders.currentApp();
function GetJsonData(callback) {
let jsonFile = documents.getFile("./shared/message.json");
jsonFile.readText().then(function (content) {
try {
var jsonData = JSON.parse(content);
callback(jsonData);
} catch (err) {
callback(err);
throw new Error('Could not parse JSON file');
}
}, function (error) {
callback(error);
throw new Error('Could not read JSON file');
});
}
function showJsonData() {
GetJsonData((array) => {
console.log(array);
});
}
你好,我正在尝试访问一个 JSON 文件到我的 nativescrpt 项目。我试过了
let fs = require("tns-core-modules/file-system");
let documents = fs.knownFolders.currentApp();
function GetJsonData(callback) {
let filePath = documents.getFile("./shared/message.json");
let array;
let jsonData;
jsonFile.readText().then(function (content) {
try {
jsonData = JSON.parse(content);
array = new observableArrayModule.ObservableArray(jsonData);
} catch (err) {
throw new Error('Could not parse JSON file');
}
}, function (error) {
throw new Error('Could not read JSON file');
});
function showJsonData() {
GetJsonData((array) => {
console.log(array);
});
}
但是无法从 JSON 文件
中获取数据您应该首先获取文件夹,并且您应该使用 knownFolders
起点执行此操作。
import { knownFolders } from 'tns-core-modules/file-system';
knownFolders.currentApp().getFolder('shared').getFile('message.json').readText ...;
如果这个文件在你的源项目中 - 你必须确保它被 webpack 保存。
在您的 webpack.config.js 文件中
new CopyWebpackPlugin([
{ from: { glob: "**/*.jpg" } },
{ from: { glob: "**/shared/*.json" } },
{ from: { glob: "**/*.png" } }
首先,您必须检查该文件是否包含在项目的 webpack.config.js 文件中。就像@dashman 说的。
在你的 webpack 中找到这个
new CopyWebpackPlugin([
{ from: { glob: "fonts/**" } },
{ from: { glob: "**/*.jpg" } },
{ from: { glob: "**/*.png" } },
], { ignore: [`${relative(appPath, appResourcesFullPath)}/**`] }),
然后改成这样
new CopyWebpackPlugin([
{ from: { glob: "fonts/**" } },
{ from: { glob: "**/*.jpg" } },
{ from: { glob: "**/shared/*.json" } },
{ from: { glob: "**/*.png" } },
], { ignore: [`${relative(appPath, appResourcesFullPath)}/**`] }),
之后你的代码也有很多错误。 改成这样
let fs = require("tns-core-modules/file-system");
let documents = fs.knownFolders.currentApp();
function GetJsonData(callback) {
let jsonFile = documents.getFile("./shared/message.json");
jsonFile.readText().then(function (content) {
try {
var jsonData = JSON.parse(content);
callback(jsonData);
} catch (err) {
callback(err);
throw new Error('Could not parse JSON file');
}
}, function (error) {
callback(error);
throw new Error('Could not read JSON file');
});
}
function showJsonData() {
GetJsonData((array) => {
console.log(array);
});
}