In NativeScript, the http
module doesn't currently support making network requests to the local file system. That work is intended to be done by the file reader. It's pretty simple to read a local file and parse its contents as JSON.
This is the TypeScript/Promise version of what Emil Oberg created for the same question on StackOverflow. This module should be reusable for any and all asyncronous local JSON read operations.
import * as fs from 'file-system';
var documents = fs.knownFolders.currentApp();
class FileReader {
static readJSON(path: string) {
var jsonFile = documents.getFile(path);
return new Promise<Object>((resolve, reject) => {
try {
jsonFile.readText().then((content: string) => {
let data = <Array<Object>>JSON.parse(content);
resolve(data);
});
}
catch (err) {
reject(err);
}
});
}
}
export default FileReader;
FWIW...I also modified Burke's original slightly. Here's what I'm using:
To use this somewhere in app code:
Hope it helps someone. Note the path to the JSON file is always relative to the root
app
directory.