Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can refer to files in the app package using URLs in the form:</p> <pre><code>ms-appx:///data/data.json </code></pre> <p>(Notice that there are three <code>/</code> characters - if you miss the third one out, you'll have problems)</p> <p>To read and parse a file that contains a JSON object, you can use the objects in the <code>Windows.Storage</code> namespace. There are three steps - to get a StorageFile object that points to the file, read the contents of the file and then parse the JSON data. Here is the code that I used to do this:</p> <pre><code>var url = new Windows.Foundation.Uri("ms-appx:///data/data.json"); Windows.Storage.StorageFile.getFileFromApplicationUriAsync(url).then(function (file) { Windows.Storage.FileIO.readTextAsync(file).then(function (text) { var parsedObject = JSON.parse(text); // do something with object }); }); </code></pre> <p>There are lots of ways of reading the data from the file, but I find the <code>FileIO</code> object the most convenient. The code above assumes there is one JSON object description in the file. If you have a file that contains one object per line, then you'll need this:</p> <pre><code>var url = new Windows.Foundation.Uri("ms-appx:///data/data.json"); Windows.Storage.StorageFile.getFileFromApplicationUriAsync(url).then(function (file) { Windows.Storage.FileIO.readLinesAsync(file).then(function (lines) { lines.forEach(function (line) { var parsedObject = JSON.parse(line); // do something with object }); }); }); </code></pre> <p>This is a slight variation that uses the <code>FileIO.readLinesAsync</code> method to create an array of strings, each of which is parsed as a JSON object.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload