最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - phonegap read and write json file - Stack Overflow

programmeradmin0浏览0评论

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

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

Share Improve this question asked Jun 21, 2012 at 11:03 NickNick 931 gold badge3 silver badges8 bronze badges 1
  • 1 why do you need mime type to store json? the data in this case will be stored in a plain text formate only – dhaval Commented Jun 21, 2012 at 11:34
Add a comment  | 

2 Answers 2

Reset to default 13

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);
发布评论

评论列表(0)

  1. 暂无评论