תשובה Way to save data to json file as and when the user types?

  • יום שישי 13 אפריל 2012 03:21
     
     

    I plan to save data ( key and value) to a local json file , as and when a user types it in the app. I need to edit (change, delete and append) in the file when the user edits old data in the app.  Also need to show all the data when the app launces again.

    is this possible?

    if so how do I use serialize to save the output to the json file.  (thinking XHR or applicatinData.localFolder ???)

    if not, what do I use to achieve this?

    Explanations / pointers please. I am unable to get it even after hunting lots of places. Thanks so much.

    karti

      

כל התגובות

  • יום שישי 13 אפריל 2012 12:08
     
     הצעה לתשובה

    There are a couple of ways to save JSON data to a file:

    First, use the JSON.stringify() function to serialize a JSON object to a string and use the FileIO class to write/read the data to/from a file.  Use the JSON.parse() function to convert a string to JSON.

    A more elaborate method would be to use IndexedDB.

    If your data is not enormous, I would operate on a JSON object in memory during the lifetime of your application.  When it suspends, then serialize and write to a file.  When the app is activated, read the file and parse back to the working JSON object.

  • יום שישי 13 אפריל 2012 22:55
     
     

    Thanks so much. I had already tried indexedDb and then switched to json to get a lesser size app.

    I will create a .resjson file and open it via fileIO class. use WinJS.application.local.readtext and writetext accordingly on local json objects in memory. Pls correct if and where needed.

    btw, does file extension name matter while reading/writing. I saw json.db in an example. (.json? .resjson? .db?)


    • נערך על-ידי ArunKarthika יום שישי 13 אפריל 2012 23:00
    •  
  • שבת 14 אפריל 2012 00:40
     
     תשובה

    Arun:

    You should first convert the JSON object that you build in your program to a string using JSON.stringify.  Then write this string to a file using the writeTextAsync methods in FileIO.  To reverse the process, read the file to a string and then convert back to a JSON object using JSON.parse.

    (I would just use a .txt extension for the file.)
    • נערך על-ידי jrboddie שבת 14 אפריל 2012 00:41
    • סומן כתשובה על-ידי ArunKarthika שבת 14 אפריל 2012 18:03
    •  
  • שבת 14 אפריל 2012 18:11
     
     

    Thankyou so much. I get it. Thanks again.

    -karthi

  • יום ראשון 17 יוני 2012 14:29
     
     

    Hi or Hola ArunKarthika

    Can I have sometrack or piece of code for write/save/edit (change, delete and append) file JSON ?

    Thx and you answer for this question http://stackoverflow.com/questions/11071473/how-to-write-save-json-using-javascript/11071600#11071600