Search code examples
c#jsonserializationjson.netwindows-store-apps

Serialize a custom List to JSON and deserialize the same to that custom list using Newtonsoft


In my Windows 8.1 app I want to store the result of my custom generated List optimally to my local storage so that I can retrieve it later on even after the app closes.
The Newtonsoft JSON serializer helps me in serializing the List to string format that I can store in any text file. But deserializing it wont fetch the required List.

Class

  public class BookDetailsItem
        {
            public string Id { get; set; }
            public ImageSource CoverImage { get; set; }
            public string Name { get; set; }
            public Visibility ProgressVisi { get; set; }
            public List<StoryDetailsItem> stories { get; set; }
        }
    public class StoryDetailsItem
        {
            public string Id { get; set; }
            public string Name { get; set; }           
            public ImageSource CoverImage { get; set; }
            public string Time { get; set; }
            public string Word { get; set; }
            public Visibility ProgressVisi { get; set; }
        }  

Now I am adding my required items to a list of this class

List<BookDetailsItem> tempbook = new List<BookDetailsItem>();
tempbook.Add(......);

Now Serializing it I can store it in any txt file

string tempstr = JsonConvert.SerializeObject(tempbook);

But deserializing it how can I get back the same List all I am getting is an object which isnt in correct format

var obj1 = (JsonConvert.DeserializeObject(tempstr));

Is there any other way in which I can store these data in Local storage more easily and optimally(without creating a SQlite database).


Solution

  • Use

    var obj1 = JsonConvert.DeserializeObject<List<BookDetailsItem>>(tempstr)
    

    To give the deserializer the type information.