简体   繁体   中英

phonegap read and write json file

I am looking to store a JSON file locally on IOS/Android in a Phonegap(Cordova) application.

Basically, I retrieve a JSON file from the server ($.GETJSON) and I want to first store the JSON file and then retrieve and modify it.

I've looked at FileWriter but I don't see a mimetype... the only example gives text files.

Thanks in advance!

Nick

Nick, just use FileWriter.write to save your JSON data to disk. JSON is a text based file anyway so there is no need to set the mime type. When you are ready to load the file again use FileReader.readAsText . In your "onloadend" handler of the FileReader the method will be called with an event and event.target.result will be your JSON data. Then you'll do a

var myJson = JSON.parse(event.target.result);

to turn the text into a JSON object.

(for template or default settings) I just store them in seperate constant files so i don't have to use any special file utility addons or commands, super easy:

(function(){
angular.module('app').constant('settings_json', 
{  
    "name":"settings",
    "data":[  
       {  
          "set":"globals",
          "share_diagnostics":true,
          "user_prefs_save_cloud": true         
       },
       {
          "set":"user",
          "fname":'',
          "lname": '',
          "telephone": '',
          "email": ''       
       }
    ]
}
)})();

Then in your app: (after injecting 'settings_json')

var settings = angular.fromJson(settings_json);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM