{"id":1939,"date":"2013-09-01T15:09:35","date_gmt":"2013-09-01T20:09:35","guid":{"rendered":"http:\/\/www.wiredprairie.us\/blog\/?p=1939"},"modified":"2013-09-01T15:09:35","modified_gmt":"2013-09-01T20:09:35","slug":"reading-a-text-file-or-json-file-from-a-windows-8-runtime-application","status":"publish","type":"post","link":"https:\/\/www.wiredprairie.us\/blog\/index.php\/archives\/1939","title":{"rendered":"Reading a text file (or JSON file) from a Windows 8 Runtime application"},"content":{"rendered":"

I just had need of reading a small JSON file into a Windows 8 application using the Windows Runtime. <\/p>\n

\n
\n
Uri uri = new<\/span> Uri("ms-appx:\/\/\/assets\/data.json"<\/span>);

var storageFile = await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(uri);

using<\/span> (var storageStream = await storageFile.OpenReadAsync())
{
using<\/span> (Stream stream = storageStream.AsStreamForRead())
{
using<\/span> (StreamReader reader = new<\/span> StreamReader(stream))
{
var jsonText = reader.ReadToEnd();
var results = JsonConvert.DeserializeObject<DemoData>(jsonText);
this<\/span>.DataContext = results;
}
}
}<\/pre>\n

What seemed like it should have been a common example on MSDN was not \u2026, and after a bit of searching and reading, I believe the above code represents at least a common and safe way of loading data. <\/div>\n<\/div>\n

I\u2019ve got a file called data.json<\/strong> stored in an Assets<\/strong> folder:<\/p>\n

\"image\"<\/a><\/p>\n

And the file\u2019s Build Action<\/strong> is set to Content:<\/strong><\/p>\n

\"image\"<\/a><\/p>\n

Change the value passed to the Uri<\/strong> constructor and \u2026 the file will be loaded successfully. Once loaded, I used the strongly typed generic DeserializeObject<\/strong> method to convert the Json data to a class called DemoData<\/strong>.<\/p>\n","protected":false},"excerpt":{"rendered":"

I just had need of reading a small JSON file into a Windows 8 application using the Windows Runtime. Uri uri = new Uri("ms-appx:\/\/\/assets\/data.json");var storageFile = await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(uri);using (var storageStream = await storageFile.OpenReadAsync()){ using (Stream stream = storageStream.AsStreamForRead()) { using (StreamReader reader = new StreamReader(stream)) { var jsonText = reader.ReadToEnd(); var results = JsonConvert.DeserializeObject<DemoData>(jsonText); this.DataContext […]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[4],"tags":[82,119,105],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/pd5QIe-vh","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":388,"url":"https:\/\/www.wiredprairie.us\/blog\/index.php\/archives\/388","url_meta":{"origin":1939,"position":0},"title":"Silverlight Weather Demonstration","date":"June 26, 2008","format":false,"excerpt":"Demonstration available here. (You'll need to wait for a moment while it loads the first time). I've created a reasonably simple, yet multi-technology (and discipline) demonstration using Silverlight for the user interface and ASP.NET as the back-end. The demonstration uses: Silverlight 2.0 Data binding Delayed downloading of images \"Web services\"\u2026","rel":"","context":"In "Coding"","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.wiredprairie.us\/blog\/wp-content\/uploads\/2008\/06\/image21.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":785,"url":"https:\/\/www.wiredprairie.us\/blog\/index.php\/archives\/785","url_meta":{"origin":1939,"position":1},"title":"A forgetful DSL modem….","date":"June 4, 2009","format":false,"excerpt":"Our household DSL modem, which is generally very reliable, occasionally, and mysteriously seems to forget a setting that I like to enable. I don\u2019t know why, and I\u2019ve never determined whether there\u2019s a pattern to the loss or not (like maybe it\u2019s every 30 days or some crazy thing like\u2026","rel":"","context":"In "Coding"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":634,"url":"https:\/\/www.wiredprairie.us\/blog\/index.php\/archives\/634","url_meta":{"origin":1939,"position":2},"title":"Converting RESTful XML output to formatted XML in Silverlight","date":"December 15, 2008","format":false,"excerpt":"Ever debugged output from a RESTful web service and was driven absolutely mad by the lack of formatting of the XML? Seriously, how much fun is it to stare at XML that has no line breaks or indentation whatsoever? I haven\u2019t tried to solve the problem within the IDE as\u2026","rel":"","context":"In "Coding"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":577,"url":"https:\/\/www.wiredprairie.us\/blog\/index.php\/archives\/577","url_meta":{"origin":1939,"position":3},"title":"Silverlight Game Programming: Playing an Audio File","date":"October 12, 2008","format":false,"excerpt":"In my recent Silverlight game, there are a variety of sound effects that occur from bullet fires to explosions. Most of the audio files are brief, lasting only for a second or less on average. I had a few options -- put a MediaElement directly on the UI (in XAML)\u2026","rel":"","context":"In "Coding"","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.wiredprairie.us\/blog\/wp-content\/uploads\/2008\/10\/image2.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":1730,"url":"https:\/\/www.wiredprairie.us\/blog\/index.php\/archives\/1730","url_meta":{"origin":1939,"position":4},"title":"How to find an element in a DataTemplate in WinRT\/XAML.","date":"September 7, 2012","format":false,"excerpt":"Here\u2019s one way to find a named element in a DataTemplate in XAML in Windows 8 XAML. You might try FindName to discover it doesn\u2019t work. That\u2019s because it\u2019s not recursive. So, I created a simple extension method to do the same thing: public static class FrameworkElementExtensions { public static\u2026","rel":"","context":"In "Coding"","img":{"alt_text":"image","src":"https:\/\/i0.wp.com\/www.wiredprairie.us\/blog\/wp-content\/uploads\/2012\/09\/image.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":1524,"url":"https:\/\/www.wiredprairie.us\/blog\/index.php\/archives\/1524","url_meta":{"origin":1939,"position":5},"title":"Alternative to ApplicationSettings in .NET","date":"February 1, 2012","format":false,"excerpt":"After dealing with lost settings, an unclear upgrade path, and my own confusion surrounding the magic of Settings in a .NET client application, I decided to build my own. You\u2019re probably familiar with this UI in Visual Studio. It hasn\u2019t changed much since it was first created: A list of\u2026","rel":"","context":"In "Coding"","img":{"alt_text":"image","src":"https:\/\/i0.wp.com\/www.wiredprairie.us\/blog\/wp-content\/uploads\/2012\/02\/image.png?resize=350%2C200","width":350,"height":200},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.wiredprairie.us\/blog\/index.php\/wpjson\/wp\/v2\/posts\/1939"}],"collection":[{"href":"https:\/\/www.wiredprairie.us\/blog\/index.php\/wpjson\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wiredprairie.us\/blog\/index.php\/wpjson\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wiredprairie.us\/blog\/index.php\/wpjson\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wiredprairie.us\/blog\/index.php\/wpjson\/wp\/v2\/comments?post=1939"}],"version-history":[{"count":1,"href":"https:\/\/www.wiredprairie.us\/blog\/index.php\/wpjson\/wp\/v2\/posts\/1939\/revisions"}],"predecessor-version":[{"id":1940,"href":"https:\/\/www.wiredprairie.us\/blog\/index.php\/wpjson\/wp\/v2\/posts\/1939\/revisions\/1940"}],"wp:attachment":[{"href":"https:\/\/www.wiredprairie.us\/blog\/index.php\/wpjson\/wp\/v2\/media?parent=1939"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wiredprairie.us\/blog\/index.php\/wpjson\/wp\/v2\/categories?post=1939"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wiredprairie.us\/blog\/index.php\/wpjson\/wp\/v2\/tags?post=1939"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}