2014-06-24 2 views
2

Code dans le programme principalJson.net erreur de désérialisation (JSONReaderException)

controller.modelToFile("passwords/test.json"); 
controller.deJSONizeModel("passwords/test.json"); 

modelToFile du contrôleur:

public void modelToFile(String filename) { 
     System.IO.File.WriteAllText(filename, JSONizeModel()); 
    } 

JSONizeModel du contrôleur:

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; 
     return Newtonsoft.Json.JsonConvert.SerializeObject(model, jss); 
    } 

de deJSonizeModel du contrôleur:

public void deJSONizeModel(String json) { 
     model = JsonConvert.DeserializeObject<Model>(json); 
    } 

Modèle:

class Model : IModel { 
    private IList<Password> passwords = new List<Password>(); 

<...> 

test.json du fichier:

{ 
    "passwords": [ 
    { 
     "description": "example password 1", 
     "password": "p1" 
    }, 
    { 
     "description": "example password 2", 
     "password": "p2" 
    }, 
    { 
     "description": "This is the password", 
     "password": "the password" 
    } 
    ] 
} 

Mot de passe classe:

public String description { get; set; } 
    public String password { get; set; } 

    public Password(String description, String password) { 
     this.description = description; 
     this.password = password; 
    } 

S'il vous plaît aider. L'exception est Newtonsoft.Json.JSONReaderException.

journal du programme:

A first chance exception of type 'Newtonsoft.Json.JsonReaderException' occurred in Newtonsoft.Json.dll 
A first chance exception of type 'Newtonsoft.Json.JsonReaderException' occurred in Newtonsoft.Json.dll 
An unhandled exception of type 'Newtonsoft.Json.JsonReaderException' occurred in Newtonsoft.Json.dll 
Additional information: Error parsing boolean value. Path '', line 0, position 0. 
+1

Pourriez-vous ajouter plus d'informations sur l'exception à la publication? InnerException le cas échéant, stacktrace peut-être. –

Répondre

2

a résolu le problème! L'argument à deJSONizeModel est un FICHIER mais JSON est requis!

+0

En fait c'était ma réponse mais thx de toute façon –

+0

Oui, c'était votre réponse, mais vous l'avez posté dans la section des questions. Je l'ai extrait et posté ici en votre nom. – gunr2171

Questions connexes