Get local json file on NativeScript

How to get local big json data?

I tried this, but I was not successful:

var sa = require("./shared/resources/sa.json");
var array = new observableArrayModule.ObservableArray(sa);
+4
source share
3 answers

Use the file-system module to read the file and then parse it with JSON.parse():

var fs = require('file-system');

var documents = fs.knownFolders.currentApp();
var jsonFile = documents.getFile('shared/resources/sa.json');
var array;
var 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');
});

Here's a real-life example of how I do this in a NativeScript application to read a large 75KB / 250,000 character large JSON file.

+7
source

TypeScript:

import {knownFolders} from "tns-core-modules/file-system";

export class Something {
    loadFile() {
        let appFolder = knownFolders.currentApp();
        let cfgFile = appFolder.getFile("config/config.json");
        console.log(cfgFile.readTextSync());
    }
}
+3
source

, . JSON data.js , , . data.js.

+2
source

Source: https://habr.com/ru/post/1606063/


All Articles