json.net - Newtonsoft.Json 反序列化失败
问题描述
重构类后,将其序列化
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>();
}
}
源/目标 JSON
{
"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]”。
解决方案
问题是我的类有一个非默认构造函数,也没有默认构造函数。错误消息无关紧要。
推荐阅读
- rust - 如何更改路径的文件名而不更改其目录或扩展名?
- reactjs - 无法编辑在通用表单组件中传递的状态对象
- terraform - 如何使用 Terraform 为 ECS 配置不同类型的部署
- java - 应用程序在从应用程序模块调用库模块的活动时崩溃
- java - 根据数字 id 获取对象
- javascript - 每种编程语言的内存结构是否不同?
- html - 如何在引导程序 3 的 div 中间放置一个按钮?
- java - 制作一个胖 jar 文件并添加依赖项 - Eclipse IDE
- r - 将多个文本标签添加到单个 facet_grid 面板
- javascript - 我有一个飞扬的小鸟游戏的开始 - 画布无法渲染/显示