Answered by:
Loading a local JSON file

Question
-
i have a folder in my project called app_data and inside is subjects.txt
inside this file is json data.
I know how to load external json as i have a twitter feed in my app but i cannot load the local json file...
WinJS.Application.local.readText("/app_data/subjects.txt").then( function (data) { var state = {}; console.log(data); try { if(data) { state = "done"; basicListView = WinJS.UI.getControl(document.getElementById("basicListView")); var theDb = JSON.parse(data); basicListView.dataSource = theDb } } catch (e) { console.dir(e); } });
Doesn't work it, the console.log(data); returns undefined.- Edited by Ashley Bowles Monday, November 12, 2012 5:00 PM
Monday, November 12, 2012 3:10 PM
Answers
-
Hi,
Please refer to the code as follow:
var uri = new Windows.Foundation.Uri("ms-appx:///app_data/myfile.data") Windows.Storage.StorageFile.getFileFromApplicationUriAsync(uri).then(function (file) { return Windows.Storage.FileIO.readTextAsync(file); }).done(function (text) { // activitiesArray = JSON.parse(text); document.getElementById("content").innerHTML = text; });
And note, you only have read authority, but write.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- Marked as answer by Song Tian Friday, November 16, 2012 1:51 AM
Tuesday, November 13, 2012 5:58 AM -
Hi Ashley,
Security is up to you.
This is nothing new to application development. If you need a secure a cryptographically encoded file you must do this yourself.
-Jeff
Jeff Sanders (MSFT)
- Marked as answer by Jeff SandersMicrosoft employee, Moderator Tuesday, November 13, 2012 1:03 PM
- Unmarked as answer by Jeff SandersMicrosoft employee, Moderator Tuesday, November 13, 2012 1:04 PM
- Proposed as answer by Jeff SandersMicrosoft employee, Moderator Tuesday, November 13, 2012 1:04 PM
- Marked as answer by Song Tian Friday, November 16, 2012 1:51 AM
Tuesday, November 13, 2012 1:03 PMModerator
All replies
-
WinJS.Application.local refers to the Application's Local AppData folder, not the applications package install folder. To read a file contained in your package, you'd need to load it directly from the install folder:
var packageFolder = Windows.ApplicationModel.Package.current.installedLocation; packageFolder.createFolderAsync("app_data", Windows.Storage.CreationCollisionOption.openIfExists).then( function (appDataFolder) { appDataFolder.createFileAsync("subjects.json", Windows.Storage.CreationCollisionOption.openIfExists).then( function (file) { Windows.Storage.FileIO.readTextAsync(file).done( function (contents) { console.log(contents); var theDb = JSON.parse(contents); }, function (error) { console.log(error); }); }, function (error) { console.log(error); }); }, function (error) { console.log(error); });
- Proposed as answer by Gearard BolandMicrosoft employee Monday, November 12, 2012 7:38 PM
Monday, November 12, 2012 7:35 PM -
Hi,
Please refer to the code as follow:
var uri = new Windows.Foundation.Uri("ms-appx:///app_data/myfile.data") Windows.Storage.StorageFile.getFileFromApplicationUriAsync(uri).then(function (file) { return Windows.Storage.FileIO.readTextAsync(file); }).done(function (text) { // activitiesArray = JSON.parse(text); document.getElementById("content").innerHTML = text; });
And note, you only have read authority, but write.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- Marked as answer by Song Tian Friday, November 16, 2012 1:51 AM
Tuesday, November 13, 2012 5:58 AM -
Dosnt work guys.
Our app will be pulling data down and saving it into files for offline use, but we want this data to be secure as it is what give us the upper hand against our competitors so i do not want to be saving data into the app data directory on the c:/ drive... i needs to be un-accessible by anyone apart from the app.
What has Microsoft done to allow developers to keep there data secure?
- Edited by Ashley Bowles Tuesday, November 13, 2012 9:53 AM
Tuesday, November 13, 2012 9:47 AM -
This still gives me the access denied error:
0x80070005 - JavaScript runtime error: Access is denied. If there is a handler for this exception, the program may be safely continued.
Tuesday, November 13, 2012 9:50 AM -
Yours give me this error:
0x80070002 - JavaScript runtime error: The system cannot find the file specified.
var uri = new Windows.Foundation.Uri("ms-appx:///data/subjects.txt") Windows.Storage.StorageFile.getFileFromApplicationUriAsync(uri).then(function (file) { return Windows.Storage.FileIO.readTextAsync(file); }).done(function (text) { // activitiesArray = JSON.parse(text); document.getElementById("content").innerHTML = text; });
- Edited by Ashley Bowles Tuesday, November 13, 2012 9:51 AM
Tuesday, November 13, 2012 9:51 AM -
Hi,
Please make sure subjects.txt under folder "data".
The error seems system can't find the file.
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 FrameworkTuesday, November 13, 2012 9:54 AM -
Right the issue was that i had the file set to resource so onc ei set it to default in properties it works. My question above still stands thoughTuesday, November 13, 2012 10:17 AM
-
Hi Ashley,
Security is up to you.
This is nothing new to application development. If you need a secure a cryptographically encoded file you must do this yourself.
-Jeff
Jeff Sanders (MSFT)
- Marked as answer by Jeff SandersMicrosoft employee, Moderator Tuesday, November 13, 2012 1:03 PM
- Unmarked as answer by Jeff SandersMicrosoft employee, Moderator Tuesday, November 13, 2012 1:04 PM
- Proposed as answer by Jeff SandersMicrosoft employee, Moderator Tuesday, November 13, 2012 1:04 PM
- Marked as answer by Song Tian Friday, November 16, 2012 1:51 AM
Tuesday, November 13, 2012 1:03 PMModerator -
Hello Ashley!
I want to do the same thing: load data from a local json file and put it in a basicListView out. What solution did helt you?
Best Regards!
Friday, July 12, 2013 9:47 AM