Store and Retrieve JSON Data From Local Storage in Windows Store App

In this tutorial we will learn step-by-step how to save and retrieve JSON data in a local folder of a Windows Store App.

Use the following procedure to create a sample of saving and retrieving JSON data in local storage.

Step 1

Right-click on the project and add the Nuget package and add the package of Json.NET.

package of Json.NET

Step 2

Let us assume that we need to store Student information in local storage. For that I have created a Student class as in the following:

  1. public class Student  
  2. {  
  3.     public string Name { getset; }  
  4.     public string City { getset; }  
  5.     public string Grade { getset; }  

Step 3

Before saving the data we need to serialize the class into JSON. We will serliaze that using the JSON.net library. After serializing the object into JSON we will save the data in a local folder of Windows Store application.

  1. string jsonContents = JsonConvert.SerializeObject(s);             
  2. StorageFolder localFolder = ApplicationData.Current.LocalFolder;  
  3. StorageFile textFile = await localFolder.CreateFileAsync("a.txt",CreationCollisionOption.ReplaceExisting);  
  4. using (IRandomAccessStream textStream = await textFile.OpenAsync(FileAccessMode.ReadWrite))  


  5.     using (DataWriter textWriter = new DataWriter(textStream))  
  6.     {  
  7.          textWriter.WriteString(jsonContents);  
  8.          await textWriter.StoreAsync();  
  9.     }  
  10.  } 

S is the object of the class Student. We are reading the student properties from various textboxes.

  1. Student s = new Student  
  2. {  
  3.     Name =txtName.Text,  
  4.     City = txtCity.Text,  
  5.     Grade =txtGrade.Text  
  6. }; 

In the above code snippet:

  1. We are saving the file in the application local folder
  2. Data is saved in file a.txt
  3. Opening the file asynchronously for the read write operations using IRandomAccessStream
  4. Using DataWriter to write the data in the file

In these four steps the data can be saved in the local folder of the Windows Store application.

Step 4

To read the JSON data back we need to first read the data from local storage and then deserliaze that to an object from JSON. Reading the data is very simple.

  1. Open the local folder
  2. Load the file asynchronously
  3. Open the file asynchronously to read the streams
  4. Use the DataReader reading stream
  5. Convert JSON to an object
    1. string jsonContents = JsonConvert.SerializeObject(s);             
    2. StorageFolder localFolder = ApplicationData.Current.LocalFolder;  
    3. StorageFile textFile = await localFolder.CreateFileAsync("a.txt",CreationCollisionOption.ReplaceExisting);  
    4. using (IRandomAccessStream textStream = await textFile.OpenAsync(FileAccessMode.ReadWrite))  
    5. {  
    6.     using (DataWriter textWriter = new DataWriter(textStream))  
    7.     {  
    8.         textWriter.WriteString(jsonContents);  
    9.         await textWriter.StoreAsync();  
    10.      }  
    11.  } 

In the preceding code snippet:

  1. We are opening the local folder
  2. Opening the file asynchronously
  3. Reading the stream in IRandonAccessStream
  4. Reading the data using DataReader
  5. DeSeralizing JSON to an object using JsonConver

Using the preceding procedure you can save and retrieve data in a local folder of Windows Store application. I hope you find this article useful.

Next Recommended Readings