locked
read a json file and return contents RRS feed

  • Question

  • Edit -  I don't think I explained my self well so I'm going try and rewrite this:


    Sorry I don't think I made my question very clear, the asynchronous function is reading from the file as if I console.log newString it spews out the json, However I can only get this data inside the function, I'm trying to to make it so it returns it, i.e

    var foo = readJson("/data/foo");

    console.log(foo);

    foo would then spew out the json, my issue is I can only get the contents of the file inside the asynchronous function not outside of it

    Old Post

    I've been having issues with making a function that reads in a json file and then returns the string. I've ripped the code out of http://peterkellner.net/2012/01/03/with-windows-8-metro-html5css3javascriptwinrt-read-any-file-from-project-directory/

        function readJson(jsonDataSrc) {
            //http://peterkellner.net/2012/01/03/with-windows-8-metro-html5css3javascriptwinrt-read-any-file-from-project-directory/
            var installedLocation = Windows.ApplicationModel.Package.current.installedLocation;

    var newString = "";
            installedLocation.createFileAsync(jsonDataSrc, Windows.Storage.CreationCollisionOption.openIfExists).then(function (dataFile) {
                dataFile.openAsync(Windows.Storage.FileAccessMode.read).then(function (stream) {
                    var size = stream.size;
                    var inputStream = stream.getInputStreamAt(0);
                    var reader = new Windows.Storage.Streams.DataReader(inputStream);

                    reader.loadAsync(size).then(function () {
                        var array = new Array(size);
                        reader.readBytes(array);


                        for (var i = 0; i < array.length; i++) {
                            if (array[i] >= 32 && array[i] <= 126) { //ascii hack
                                var c = String.fromCharCode(array[i]);
                                newString += c;
                            }
                        }

                    })
                })
            });
            return JSON.parse(newString);
        }

    my problem is that  newString does not return whats in the file but instead it would return "", after looking at google I think this is to do with it being asynchronous, any help on the matter would be awesome.




    • Edited by zidsal Wednesday, October 31, 2012 12:06 PM
    Tuesday, October 30, 2012 6:47 PM

Answers

  • Hi,

    Because this is an asynchronous function. Generally, we don't use return. Instead using a global variable.

    For example:

            var feedItems;
            function readJson(jsonDataSrc) {          
                var installedLocation = Windows.ApplicationModel.Package.current.installedLocation;    
                var newString = "";       
                installedLocation.createFileAsync(jsonDataSrc, Windows.Storage.CreationCollisionOption.openIfExists).then(function (dataFile) {   
                    dataFile.openAsync(Windows.Storage.FileAccessMode.read).then(function (stream) {    
                        var size = stream.size;            
                        var inputStream = stream.getInputStreamAt(0);         
                        var reader = new Windows.Storage.Streams.DataReader(inputStream);       
                        reader.loadAsync(size).then(function () {                
                            var array = new Array(size);                  
                            reader.readBytes(array);                 
                            for (var i = 0; i < array.length; i++) {         
                                if (array[i] >= 32 && array[i] <= 126) { //ascii hack    
                                    var c = String.fromCharCode(array[i]);
                                    newString += c;
                                    
                                }
                            }
                            DataUtils.feedItems = newString;
                        })
                    })
                });
            }
    
            WinJS.Namespace.define(
           "DataUtils", {
               feedItems: feedItems
           }
           );


    Please mark the replies as answers if they help or unmark if not.
    If you have any feedback about my replies, please contact msdnmg@microsoft.com
    Microsoft One Code Framework

    Thursday, November 1, 2012 2:43 AM

All replies

  • Hi,

    Please make sure the json file in the related folder.

    The folder should be

    installedLocation.path

    You could have a debug, and will find that.

    For example, add the below code:

    document.getElementById('Div1').innerHTML = installedLocation.path;


    Please mark the replies as answers if they help or unmark if not.
    If you have any feedback about my replies, please contact msdnmg@microsoft.com
    Microsoft One Code Framework

    Wednesday, October 31, 2012 9:44 AM
  • Sorry I don't think I made my question very clear, the asynchronous function is reading from the file as if I console.log newString it spews out the json, However I can only get this data inside the function, I'm trying to to make it so it returns it, i.e

    var foo = readJson("/data/foo");

    console.log(foo);

    foo would then spew out the json, my issue is I can only get the contents of the file inside the asynchronous function not outside of it

    Wednesday, October 31, 2012 12:05 PM
  • Hi,

    Because this is an asynchronous function. Generally, we don't use return. Instead using a global variable.

    For example:

            var feedItems;
            function readJson(jsonDataSrc) {          
                var installedLocation = Windows.ApplicationModel.Package.current.installedLocation;    
                var newString = "";       
                installedLocation.createFileAsync(jsonDataSrc, Windows.Storage.CreationCollisionOption.openIfExists).then(function (dataFile) {   
                    dataFile.openAsync(Windows.Storage.FileAccessMode.read).then(function (stream) {    
                        var size = stream.size;            
                        var inputStream = stream.getInputStreamAt(0);         
                        var reader = new Windows.Storage.Streams.DataReader(inputStream);       
                        reader.loadAsync(size).then(function () {                
                            var array = new Array(size);                  
                            reader.readBytes(array);                 
                            for (var i = 0; i < array.length; i++) {         
                                if (array[i] >= 32 && array[i] <= 126) { //ascii hack    
                                    var c = String.fromCharCode(array[i]);
                                    newString += c;
                                    
                                }
                            }
                            DataUtils.feedItems = newString;
                        })
                    })
                });
            }
    
            WinJS.Namespace.define(
           "DataUtils", {
               feedItems: feedItems
           }
           );


    Please mark the replies as answers if they help or unmark if not.
    If you have any feedback about my replies, please contact msdnmg@microsoft.com
    Microsoft One Code Framework

    Thursday, November 1, 2012 2:43 AM
  • Hello Song Tian,

    i have a similar problem like zidsal. I want to access the "newString" from the example above outside the function. So I define the Namespace like you did. Is the "newString" then stored in "feedItems"? Can I do something like this:

      var feedItems;
            function readJson(jsonDataSrc) {          
                var installedLocation = Windows.ApplicationModel.Package.current.installedLocation;    
                var newString = "";       
                installedLocation.createFileAsync(jsonDataSrc, Windows.Storage.CreationCollisionOption.openIfExists).then(function (dataFile) {   
                    dataFile.openAsync(Windows.Storage.FileAccessMode.read).then(function (stream) {    
                        var size = stream.size;            
                        var inputStream = stream.getInputStreamAt(0);         
                        var reader = new Windows.Storage.Streams.DataReader(inputStream);       
                        reader.loadAsync(size).then(function () {                
                            var array = new Array(size);                  
                            reader.readBytes(array);                 
                            for (var i = 0; i < array.length; i++) {         
                                if (array[i] >= 32 && array[i] <= 126) { //ascii hack    
                                    var c = String.fromCharCode(array[i]);
                                    newString += c;
                                    
                                }
                            }
                            DataUtils.feedItems = newString;
                        })
                    })
                });
            }
    
            WinJS.Namespace.define(
           "DataUtils", {
               feedItems: feedItems
           }
           );
    
    var obj = JSON.parse(feedItems);
    var fillTitle = obj.title;
    var fillDescription = obj.description;
    
    document.getElementById('Div1').innerHTML = fillTitle;
    document.getElementById('Div2').innerHTML = fillDescription;
    Best regards

    Friday, July 12, 2013 1:59 PM