locked
Fill the List with Data from Json in Javascript RRS feed

  • Question

  • Hi! I am a making a small Windows App where one of the pages should show a List of Data, taken from Json. Just like in this example:  http://msdn.microsoft.com/en-us/library/windows/apps/hh465496.aspx. Only instead of a ready DataArray I woulld like to retrieve the Data from a local Json-File, which lies in a subfolder of another folder. Here is my JS-code:

    (function () { "use strict"; var feedItems; var someFolder = Windows.ApplicationModel.Package.current.installedLocation; var newString = ""; var obj; someFolder.createFolderAsync("mainFolder", Windows.Storage.CreationCollisionOption.openIfExists).then (function (mainFolder) { mainFolder.createFileAsync("myJson.json", Windows.Storage.CreationCollisionOption.openIfExists).then (function (myJsonFile) { myJsonFile.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) { var c = String.fromCharCode(array[i]); newString += c; } } //DataUtils.feedItems = newString; //document.getElementById('Div2').innerHTML = newString; //var myObject = eval('(' + newString + ')'); //document.getElementById('Div2').innerHTML = myObject; //DataUtils.feedItems = myObject;

    var obj = JSON.parse(newString); /*document.getElementById('Div1').innerHTML = obj.title.de; document.getElementById('Div2').innerHTML = obj.description.de;*/ DataUtils.feedItems = obj; }) }) }); }); WinJS.Namespace.define("DataUtils", { feedItems: feedItems }); /*var dataArray = [ { title: "Basic banana", text: "Low-fat frozen yogurt", picture: "images/60banana.png" }, { title: "Banana blast", text: "Ice cream", picture: "images/60banana.png" }, { title: "Brilliant banana", text: "Frozen custard", picture: "images/60banana.png" }, { title: "Orange surprise", text: "Sherbet", picture: "images/60orange.png" }, { title: "Original orange", text: "Sherbet", picture: "images/60orange.png" }, { title: "Vanilla", text: "Ice cream", picture: "images/60vanilla.png" }, { title: "Very vanilla", text: "Frozen custard", picture: "images/60vanilla.png" }, { title: "Marvelous mint", text: "Gelato", picture: "images/60mint.png" }, { title: "Succulent strawberry", text: "Sorbet", picture: "images/60strawberry.png" } ];*/ var dataList = new WinJS.Binding.List(obj); // Create a namespace to make the data publicly // accessible. var publicMembers = { itemList: dataList }; WinJS.Namespace.define("DataExample", publicMembers); })();

    And the html-code:

    <div id="basicListView" 
                data-win-control="WinJS.UI.ListView"
                data-win-options="{ itemDataSource : DataExample.itemList.dataSource }"> 
    </div>
           

    The two problems are: I can't open a subfolder in a folder and dataList won't show the object.

    Can anyone tell me where my errors are.

    Thanx!

    Monday, July 8, 2013 10:06 AM

All replies

  • Hello,

    Windows.ApplicationModel.Package.current.installedLocation retrieves the app's install directory(bin\debug\Appx ), and it readonly, if you  use createFolderAsync() method to create a new sub-folder inside it, the system will report 'access is denied' error. I would like to suggest choose appropriate method to retrieve file  from your app's  install directory in the following link.

    http://msdn.microsoft.com/en-us/library/windows/apps/hh967755.aspx

    Hope this helps, thanks.


    Yanping Wang
    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.

    Tuesday, July 9, 2013 3:23 AM
    Moderator
  • Hello Yanping!

    Thank you for your reply! But I do get the output in a div with my code as it is now. The problem is when I try to output the json data in a basicListView.

    Best regards.

    Friday, July 12, 2013 10:35 AM
  • Any Idea? Someone?
    Thursday, July 18, 2013 1:01 PM