【发布时间】:2018-05-15 05:38:13
【问题描述】:
我在 C# 中使用 Newtonsoft JSON 解析器。 JSON 似乎格式正确,但我收到一个没有意义的读取错误。 JSON 应该自动反序列化并加载到类实例中。
类:
class FilterMatrix {
public int ID { get; set; }
public int ParentID { get; set; }
}
反序列化代码:
string fileName = @"C:\Users\accounts.json";
FilterMatrix kernel = JsonConvert.DeserializeObject<FilterMatrix>(File.ReadAllText(fileName));
JSON 文件内容:
{"Features":[{"ID":0,"ParentID":0},{"ID":0,"ParentID":0}]}
错误:
Newtonsoft.Json.JsonReaderException: 'Unexpected character encountered while parsing value: {. Path 'Features', line 1, position 14.'
【问题讨论】:
-
错误信息似乎有点奇怪,但 JSON 看起来并不代表 FilterMatrix 对象
-
我刚刚意识到我的班级需要一个 Features 属性。它应该是集合类型吗?列表?