Introduction

This blog is mainly focused on SharePoint
I will use this to store and share all the tips and tricks about SharePoint and the related products
List of all posts

Save / Load JSON Object from file in METRO Application


Topic Save / Load JSON Object from file in METRO Application
OutputThe dedicated functions

Step Description
1 Saving the JSON file
In this sample we are saving the basic info of a user
function RegisterUser() {
    Windows.Storage.ApplicationData.current.localFolder.createFileAsync("User.json", Windows.Storage.CreationCollisionOption.openIfExists).done(
        Windows.Storage.ApplicationData.current.localFolder.getFileAsync("User.json").done(
            function (fjson){
                var TabSexe = document.getElementsByName("sex");
                var sexe;
                for (var i = 0; i < TabSexe.length; i++) {
                    if (TabSexe[i].checked) {
                        sexe = TabSexe[i].value;
                    }
                }
                var UserJSON = { "Pseudo": document.getElementById("txtPseudo").value, "BirthDate": document.getElementById("ddlDateNaissance").value, "Sexe": sexe };
                Windows.Storage.FileIO.writeTextAsync(fjson, JSON.stringify(UserJSON));
            }
        )
    )
}
 
2 Loading the JSON file
In this example, we load the JSON object from a file and navigate in it.
function showSessions() {
    Windows.Storage.ApplicationData.current.localFolder.getFileAsync("Sessions.json").done(
            function (getSessions) {
                Windows.Storage.FileIO.readTextAsync(getSessions).done(
                    function (jSessions) {
                        var SessionJSON = eval("(" + jSessions + ")");
                        var ttd = document.getElementById("placer_one");
                        ttd.innerHTML = SessionJSON.Training.Type + " : " + SessionJSON.Training.Id + " : " + SessionJSON.Training.Target;
                        var ttable = document.getElementById("EntTable");
                        for (var i = 0; i < SessionJSON.Sessions.length; i++){
                            tr = document.createElement("tr");
                           // {"Id": "1", "Distance" : "1000m", "TargetTime" : "12m","RealTime" : null,"ResultType" : null,"Eval" : null},
                            td = document.createElement("td");
                            td.innerHTML = SessionJSON.Sessions[i].Id;
                            tr.appendChild(td);
                            td = document.createElement("td");
                            td.innerHTML = SessionJSON.Sessions[i].Distance;
                            tr.appendChild(td);
                            td = document.createElement("td");
                            td.innerHTML = SessionJSON.Sessions[i].TargetTime;
                            tr.appendChild(td);
                            td = document.createElement("td");
                            td.innerHTML = SessionJSON.Sessions[i].RealTime;
                            tr.appendChild(td);
                            td = document.createElement("td");
                            td.innerHTML = SessionJSON.Sessions[i].ResultType;
                            tr.appendChild(td);
                            td = document.createElement("td");
                            td.innerHTML = SessionJSON.Sessions[i].Eval;
                            tr.appendChild(td);
                            ttable.appendChild(tr);
                        }
                    }
                )
            }
        )
}
 

No comments:

Post a Comment

by Category