locked
Invalid pointer exception while writing json object to file using javascript in windows store app

    Question

  • I am trying to write these 3 arrays arrb,arr,arrc to a file as json object which i will read later but when i am calling save() function it throws an invalid pointer exception in this line of code-

    Windows.Storage.FileIO.writeTextAsync(sampleFile, str1.toString() + "" + str2.toString() + "" + str3.toString()).then(function () {
                // Add code to do something after the text is written to the file
            });

    I tried it without .toString() but not worked but when I tried writing simple text in file it works perfectly. I got these codes from msdn website and I am trying use these in my application but I am not able to overcome this exception here so please help me how I can solve this issue. Below is my complete code:

    <script type="text/javascript">
        var sampleFile;
        var arrb =[1, 2, 3, 4, 5, 6, 7, 8, 9];
        var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
        var arrr =[1, 2, 3, 4, 5, 6, 7, 8, 9];
        var str1="a", str2="d", str3="c";
        str1 = JSON.stringify(arr);
        str2 = JSON.stringify(arrb);
        str3 = JSON.stringify(arrr);
        function save() {
            Windows.Storage.KnownFolders.documentsLibrary.createFileAsync("savegame.txt",
        Windows.Storage.CreationCollisionOption.replaceExisting).then(function (file) {
            sampleFile = file;
        });
            Windows.Storage.FileIO.writeTextAsync(sampleFile, str1.toString() + "" + str2.toString() + "" + str3.toString()).then(function () {
                // Add code to do something after the text is written to the file
            });
        }
    </script>
    </head>
    <body>
    <input type="button" value="save" onclick="save();" />
    </body>

    Friday, January 04, 2013 4:20 AM

Answers

  • Hi,

    You should store the data as string in a file. And then convert to json when using.

    For example:

    var  str="{'strv':[{'a':'a11'}, {'a':'b222'} ]}"; //string

    var myJSONObject2=JSON.parse(str); //convert to json

    var jsonob={'jsonv':[ {'j':'j111'}, {'j':'j222'} ]};//json object

    var mystr2=JSON.stringify(jsonob); //convert to string


    Roy
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Song Tian Thursday, January 10, 2013 9:31 AM
    Friday, January 04, 2013 7:38 AM