locked
read a local json file in javascript

    Question

  • Hello! I have a navigation app and I want to read a json file and to put it on one of the pages out. Here is my code:

    (function () {
        "use strict";
    
        WinJS.UI.Pages.define("/pages/someFolder/somePage.html", {
            ready: function (element, options) {
            },
          
            readJson: function() {
                var packageFolder = Windows.ApplicationModel.Package.current.installedLocation;
                packageFolder.createFolderAsync("folderWithJson", Windows.Storage.CreationCollisionOption.openIfExists).then(
                    function (appDataFolder) {
                        appDataFolder.createFileAsync("myJson.json", Windows.Storage.CreationCollisionOption.openIfExists).then(
                            function (file) {
                                Windows.Storage.FileIO.readTextAsync(file).done(
                                    function (contents) {
                                        console.log(contents);
                                        var myJson = JSON.parse(contents);
                                        document.getElementById("someElement").innerHTML = myJson;
                                    },
                                    function (error) {
                                        console.log(error);
                                    });
                            },
                            function (error) {
                                console.log(error);
                            });
                    },
                    function (error) {
                        console.log(error);
                    });
            },
           
            unload: function () {...

    What am I doing wrong? I don't even get a console output.

    Best regards!

    Thursday, July 4, 2013 10:12 AM

Answers

All replies

  • Hello,

    To see the JavaScript console, you should select DEBUG > Windows > JavaScript Console in the Visual Studio menus.

    At first, I would like to suggest you make breakpoint to debug your code, so that you can check which line breaks.

    also, please make sure your json file has placed in the correspondingpath and the folderWithJson folder exist.

    details about Windows.ApplicationModel.Package.current.installedLocation you can refer

    http://msdn.microsoft.com/en-us/library/windows/apps/windows.applicationmodel.package.installedlocation

    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.

    Friday, July 5, 2013 8:41 AM
    Moderator
  • Hello and thank you for your reply!

    I can see my javascript-console, but it doesn't show any errors or the output I want it to show...

    I will now make some breakpoints and debug it...

    Saturday, July 6, 2013 8:20 AM