none
Way to save data to json file as and when the user types?

    Question

  • 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

      
    Friday, April 13, 2012 3:21 AM

Answers

  • 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.)
    Saturday, April 14, 2012 12:40 AM

All replies

  • 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.

    Friday, April 13, 2012 12:08 PM
  • 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?)


    Friday, April 13, 2012 10:55 PM
  • 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.)
    Saturday, April 14, 2012 12:40 AM
  • Thankyou so much. I get it. Thanks again.

    -karthi

    Saturday, April 14, 2012 6:11 PM
  • 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

    Sunday, June 17, 2012 2:29 PM