简体   繁体   中英

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).

Use

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

To give the deserializer the type information.

it's not possible to serialize an ImageSource object.

try saving it as a byte[]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM