Get a local json file on NativeScript

后端 未结 4 876
伪装坚强ぢ
伪装坚强ぢ 2021-01-18 07:29

How to get a local big json data?

I have tried this, but I had no success:

var sa = require(\"./shared/resources/sa.json\");
var array = new observab         


        
4条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-18 08:09

    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'm doing it in a NativeScript app to read a 75kb/250 000 characters big JSON file.

提交回复
热议问题