Windows 8–Conference Buddy. Reading JSON From A File

I’d like to have a page in Conference Buddy that lists all the customer contacts. We’re currently writing these contacts to file in JSON format as described in this article.

My goal for today is to read that file and populate a ListView with the contents, using data binding.

To get started, I need to ensure that my JSON file is right. Until now, I’ve been adding new records one by one to the file, but that won’t give me correct JSON. The right solution is to keep a list of all the customers in memory, append to that list when new records are added and then write that list to file, so that we get the correct brackets, etc.

We can then read that file back into a collection class and bind to that class to populate a ListView.

JsonConvert will convert any .NET collection to a JSON array, which is just what we want. We then write that array to the file, just as we previously wrote a single record to the file. The one key difference is that when we create the file in the VM we are careful to set the CreationCollisionOption on the file to ReplaceExisting so that we are not appending one list onto another, but instead replacing the old list with the new list.

Opening the File and Reading

The code for opening the file is very similar to the code for creating the file. First we make sure the CurrentFolder (that points to Documents) isn’t null. We then call CreateFolderAsync on the subfolder, opening it if it exists (which it will). Finally, we call GetFileAsync on that subfolder, passing in the name of the file we wish to open; what we get back is a StorageFile object for that file.

//TODO: Handle if file does not existreturn await folder.GetFileAsync(fileName); }elsereturnnull;}

We create a method ReadFromFile in our code behind which we will call to obtain the JSON string so that we can populate the list box. Its job is to call the ViewModel’s LoadDocumentsLibrary to set up the read and then to call the OpenFile method with the correct file names. Once we have the file open we can call ReadTextAsync on the FileIO object, passing in the name of the file and getting back its contents as a string.

We want to populate the list box as soon as we get to the page, so we add a call to PopulateListBox in the OnNavigatedTo method. PoopulateListBox just calls ReadFromFile and then asks JsonConvert to Deserialize the object back into a collection. We then set the View Model’s Customers property to that collection,

There is considerably more we can and will do to make this look better, but the essential capabilities are now in place to persist our collection to file and to read it from a file into a ListView or other collection display.

About Jesse Liberty

Jesse Liberty is a Senior Consultant with Wintellect, with three decades of experience writing and delivering software projects. He is the author of 2 dozen books and multiple Pluralsight & LinkedIn Learning courses, and has been a Senior Technical Evangelist for Microsoft, a Distinguished Software Engineer for AT&T, a VP for Information Services for Citibank and a Software Architect for PBS. He is a Xamarin Certified Mobile Developer and a Xamarin MVP and a Microsoft MVP.