c# - Json.net 反序列化错误(JSONReaderException)

标签 c# .net json json.net json-deserialization

主程序中的代码

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

Controller 的modelToFile:

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

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

Controller 的 deJSonizeModel:

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

型号:

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

<...>

文件 test.json:

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

密码类别:

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

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

请帮忙。异常(exception)是 Newtonsoft.Json.JSONReaderException。

程序日志:

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.

最佳答案

解决了! deJSONizeModel 的参数是一个 FILE,但 JSON 是必需的!

关于c# - Json.net 反序列化错误(JSONReaderException),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24389648/

相关文章:

json - jq - 根据子元素的搜索结果查找父元素

python - 使用 Bottle.py 返回排序后的 JSON

c# - Json.Net 中的强类型 Object[]

software-lifecycle - Aldon 和 .Net 开发

c# - Parallel.ForEach 比 ForEach 慢

c# - CA2227 的解决方案或更好的方法?

c# - 版本 12+ 中是否有 Azure.Storage.Queue 的 ContinuationToken

.net - 如何将巨大(但简单)的分层 XML 文件加载到 SQL 表中

c# - .NET 开源是 Linux 和 Mac 上可用的 MS 运行时吗?

c# - 如何在C#中获取类中的变量列表