deserializing json C# "First chance exception of type - newtonsoft" -


i trying save , load files isolatedstorage based on this class forum member shawn kendrot got throughout this forum post.

i’m able save no problem when loading deserialising json file getting error

“a first chance exception of type 'newtonsoft.json.jsonserializationexception'  occurred in newtonsoft.json.dll” 

i don’t know doing wrong, because file saved , it’s reading not deserializing. json file has 4 entries @ moment, have 6 later on.

can me understand wrong here?

it's function:

public static t readshareddata<t>(string filename) t : class, new()     {         t result = new t();         var mutex = getmutex(filename);         mutex.waitone();         filename = getsharedfilename(filename);         try         {             var storage = isolatedstoragefile.getuserstoreforapplication();             if (storage.fileexists(filename))             {                 using (var filestream = storage.openfile(filename, filemode.open, fileaccess.read))                 {                     using (var reader = new streamreader(filestream))                     {                         string json = reader.readtoend();                         if (string.isnullorempty(json) == false)                         {                             var data = jsonconvert.deserializeobject<t>(json);                             if (data != null)                             {                                 result = data;                             }                         }                     }                 }             }         }         catch { }                 {             mutex.release();         }         return result;     } 

the problem occurring on line:

var data = jsonconvert.deserializeobject<t>(json); 

my mainpage.xaml.cs

using microsoft.phone.shell; using json_storage_test.resources; using system.diagnostics; using system.text; using system.io.isolatedstorage;  namespace json_storage_test { public partial class mainpage : phoneapplicationpage {     private const string filename = “moviesettings.json";      // constructor     public mainpage()     {         initializecomponent();     }      private void savej_click(object sender, routedeventargs e)     {         string json = @"{                           'name': 'bad boys',                           'releasedate': '1995-4-7t00:00:00',                           'genres': [ 'action', 'comedy' ]                         }";          filestorage.writeshareddata("moviesettings.json", json);     }      //im not sure here, should return type     //and how access retrieved data     private void loadj_click(object sender, routedeventargs e)     {         filestorage.readshareddata<mainpage>(filename);         debug.writeline("load clicked");     }   } } 

your problem - pointed out - have no clue return value.

filestorage.readshareddata<mainpage>(filename); tries parse json string object of type mainpage should have properties name, releasedate , genres (which should ienumerable).

your mainpage has no such properties, deserialization crashes.

try following:

public class move {     public string name;     public string releasedate;     public ienumerable<string> genres } 

and call var deserializedmove = filestorage.readshareddata<movie>(filename);


Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -