c# - Json.net deserialization error (JSONReaderException) -
code in main program
controller.modeltofile("passwords/test.json"); controller.dejsonizemodel("passwords/test.json"); controller's modeltofile:
public void modeltofile(string filename) { system.io.file.writealltext(filename, jsonizemodel()); } controller's jsonizemodel:
public string jsonizemodel() { newtonsoft.json.jsonserializersettings jss = new newtonsoft.json.jsonserializersettings(); jss.formatting = newtonsoft.json.formatting.indented; newtonsoft.json.serialization.defaultcontractresolver dcr = new newtonsoft.json.serialization.defaultcontractresolver(); dcr.defaultmemberssearchflags |= system.reflection.bindingflags.nonpublic; jss.contractresolver = dcr; homecoming newtonsoft.json.jsonconvert.serializeobject(model, jss); } controller's dejsonizemodel:
public void dejsonizemodel(string json) { model = jsonconvert.deserializeobject<model>(json); } model:
class model : imodel { private ilist<password> passwords = new list<password>(); <...> file test.json:
{ "passwords": [ { "description": "example password 1", "password": "p1" }, { "description": "example password 2", "password": "p2" }, { "description": "this password", "password": "the password" } ] } password class:
public string description { get; set; } public string password { get; set; } public password(string description, string password) { this.description = description; this.password = password; } please help. exception newtonsoft.json.jsonreaderexception.
program log:
a first chance exception of type 'newtonsoft.json.jsonreaderexception' occurred in newtonsoft.json.dll first chance exception of type 'newtonsoft.json.jsonreaderexception' occurred in newtonsoft.json.dll unhandled exception of type 'newtonsoft.json.jsonreaderexception' occurred in newtonsoft.json.dll additional information: error parsing boolean value. path '', line 0, position 0.
solved it! argument dejsonizemodel file json required!
c# .net json json.net json-deserialization
No comments:
Post a Comment