Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 675
Nick Avatar asked Jun 21 '12 11:06

Nick


1 Answers

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.

like image 200
Simon MacDonald Avatar answered Nov 03 '22 01:11

Simon MacDonald