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.
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:
- public class Student
- {
- public string Name { get; set; }
- public string City { get; set; }
- public string Grade { get; set; }
- }
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.
- string jsonContents = JsonConvert.SerializeObject(s);
- StorageFolder localFolder = ApplicationData.Current.LocalFolder;
- StorageFile textFile = await localFolder.CreateFileAsync("a.txt",CreationCollisionOption.ReplaceExisting);
- using (IRandomAccessStream textStream = await textFile.OpenAsync(FileAccessMode.ReadWrite))
- {
using (DataWriter textWriter = new DataWriter(textStream))
- {
- textWriter.WriteString(jsonContents);
- await textWriter.StoreAsync();
- }
- }
S is the object of the class Student. We are reading the student properties from various textboxes.
- Student s = new Student
- {
- Name =txtName.Text,
- City = txtCity.Text,
- Grade =txtGrade.Text
- };
In the above code snippet:
- We are saving the file in the application local folder
- Data is saved in file a.txt
- Opening the file asynchronously for the read write operations using IRandomAccessStream
- 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.
- Open the local folder
- Load the file asynchronously
- Open the file asynchronously to read the streams
- Use the DataReader reading stream
- Convert JSON to an object
- string jsonContents = JsonConvert.SerializeObject(s);
- StorageFolder localFolder = ApplicationData.Current.LocalFolder;
- StorageFile textFile = await localFolder.CreateFileAsync("a.txt",CreationCollisionOption.ReplaceExisting);
- using (IRandomAccessStream textStream = await textFile.OpenAsync(FileAccessMode.ReadWrite))
- {
- using (DataWriter textWriter = new DataWriter(textStream))
- {
- textWriter.WriteString(jsonContents);
- await textWriter.StoreAsync();
- }
- }
}
In the preceding code snippet:
- We are opening the local folder
- Opening the file asynchronously
- Reading the stream in IRandonAccessStream
- Reading the data using DataReader
- 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.