重构一个类后,用
对其进行序列化file.Write(JsonConvert.SerializeObject(this, Formatting.Indented));
按预期工作,并生成本期中描述的json文件。 但是,反序列化不再有效。
http://json2csharp.com能够正确检索对象结构。
public class VinciModel
{
public string Name { get; set; }
public string VidiWorkspaceDir { get; set; }
public string ViDiWorkspaceName { get; set; }
public int NRows { get; set; }
public List<List<object>> Rows { get; set; }
public int NChocolates { get; set; }
public List<StreamInfo> StreamList { get; set; }
public float BlisterWidth { get; set; }
public float BlisterHeight { get; set; }
}
public class Socket
{
public int Index { get; set; }
public string StreamName { get; set; }
public Rectangle ROI { get; set; }
}
public class StreamInfo
{
public string StreamName { get; set; }
public List<Socket> Sockets { get; set; } = new List<Socket>();
}
}
{
"Name": "sdgsd",
"VidiWorkspaceDir": "D:\\VidiProjects\\VidiWorkspace-demo",
"ViDiWorkspaceName": "box2shrink_2",
"NRows": 1,
"Rows": [
[
{
"Index": 0,
"StreamName": "mystream",
"ROI": "0, 0, 5, 51"
},
{
"Index": 0,
"StreamName": "1",
"ROI": "5, 0, 5, 51"
},
{
"Index": 0,
"StreamName": "2",
"ROI": "11, 0, 5, 51"
},
{
"Index": 0,
"StreamName": "mystream",
"ROI": "17, 0, 5, 51"
}
]
],
"NChocolates": 4,
"StreamList": [
{
"StreamName": "mystream",
"Sockets": [
{
"Index": 0,
"StreamName": "mystream",
"ROI": "0, 0, 5, 51"
},
{
"Index": 0,
"StreamName": "mystream",
"ROI": "17, 0, 5, 51"
}
]
},
{
"StreamName": "1",
"Sockets": [
{
"Index": 0,
"StreamName": "1",
"ROI": "5, 0, 5, 51"
}
]
},
{
"StreamName": "2",
"Sockets": [
{
"Index": 0,
"StreamName": "2",
"ROI": "11, 0, 5, 51"
}
]
}
],
"BlisterWidth": 23.0,
"BlisterHeight": 51.0
}
Newtonsoft.Json.JsonReaderException: '解析值{时遇到意外字符。路径“行[0]”,第8行,位置7。”
using (var file = new System.IO.StreamReader(path))
JsonConvert.DeserializeObject<VinciModel>(file.ReadToEnd());
在重构课程之前就工作过
我也尝试过:
var obj = JsonConvert.DeserializeObject<JObject>(file.ReadToEnd());
得到了: '读取字符串时出错。意外的令牌:StartObject。路径“行[0] [0]”。
答案 0 :(得分:0)
问题是我的班级有一个非默认构造函数,也没有默认构造函数。该错误消息无关紧要。