JSON.NET class 的反序列化包含 classes 的列表
JSON.NET deserialization of class containing a list of classes
所以我正在尝试反序列化 class“MyDoc”,其中包含一个 classes 列表(有问题的是 MyPolyline class 实体列表),就像这样
class MyPoint2d
{
public double X { get; set; }
public double Y { get; set; }
[JsonConstructor]
public MyPoint2d(double x, double y)
{
X = x;
Y = y;
}
}
class MyPolyline
{
public List<MyPoint2d> Points { get; set; }
[JsonConstructor]
public MyPolyline(List<Point2d> points)
{
Points = new List<MyPoint2d>();
foreach (Point2d p in points)
{
Points.Add(new MyPoint2d(p));
}
}
}
class MyLine
{
public MyPoint2d StartPoint { get; set; }
public MyPoint2d EndPoint { get; set; }
[JsonConstructor]
public MyLine(Point2d st, Point2d ed)
{
StartPoint = new MyPoint2d(st);
EndPoint = new MyPoint2d(ed);
}
}
class MyDoc
{
public List<MyLine> Lines { get; set; }
public List<MyPolyline> Polylines { get; set; }
}
(Point2d class 来自 AutoCad 几何图形,但我使用“MyPoint2d”来限制导出到 json 的属性数量)
所以在用标准
转换文件后
MyDoc deserialized = JsonConvert.DeserializeObject<MyDoc>(jsonFileContent);
之后的 MyLine classes 列表工作正常,所有属性都保持正确的值,但通过 MyPolyline 列表 returns 正确数量的实体有一个问题,就是所有点的属性 X 和 Y 都为零。
json 文件本身没问题,我真的想不出任何办法让这个东西工作。以防万一 json 文件结构相当明显,但它看起来像这样:
{
"Lines": [
{
"StartPoint": {
"X": 1594.9640653785937,
"Y": 1490.1708760910014
},
"EndPoint": {
"X": 1455.6957137348581,
"Y": 1381.9184832054661
}
}
],
"Polylines": [
{
"Points": [
{
"X": 2155.1322935779444,
"Y": 2022.1540617522687
},
{
"X": 2291.3057975869833,
"Y": 1728.326139136384
}
]
}
]
}
所以问题是 - 有什么好的方法来处理这个问题吗?
如果您的变量名与 json 文件中的相同,您可以删除 json属性 属性。
class MyPoint2d
{
[JsonProperty("X")]
public double X { get; set; }
[JsonProperty("Y")]
public double Y { get; set; }
}
class MyPolyline
{
[JsonProperty("Points")]
public List<MyPoint2d> Points { get; set; }
}
class MyLine
{
[JsonProperty("StartPoint")]
public MyPoint2d StartPoint { get; set; }
[JsonProperty("EndPoint")]
public MyPoint2d EndPoint { get; set; }
}
class MyDoc
{
[JsonProperty("Lines")]
public List<MyLine> Lines { get; set; }
[JsonProperty("Polylines")]
public List<MyPolyline> Polylines { get; set; }
}
所以我正在尝试反序列化 class“MyDoc”,其中包含一个 classes 列表(有问题的是 MyPolyline class 实体列表),就像这样
class MyPoint2d
{
public double X { get; set; }
public double Y { get; set; }
[JsonConstructor]
public MyPoint2d(double x, double y)
{
X = x;
Y = y;
}
}
class MyPolyline
{
public List<MyPoint2d> Points { get; set; }
[JsonConstructor]
public MyPolyline(List<Point2d> points)
{
Points = new List<MyPoint2d>();
foreach (Point2d p in points)
{
Points.Add(new MyPoint2d(p));
}
}
}
class MyLine
{
public MyPoint2d StartPoint { get; set; }
public MyPoint2d EndPoint { get; set; }
[JsonConstructor]
public MyLine(Point2d st, Point2d ed)
{
StartPoint = new MyPoint2d(st);
EndPoint = new MyPoint2d(ed);
}
}
class MyDoc
{
public List<MyLine> Lines { get; set; }
public List<MyPolyline> Polylines { get; set; }
}
(Point2d class 来自 AutoCad 几何图形,但我使用“MyPoint2d”来限制导出到 json 的属性数量)
所以在用标准
MyDoc deserialized = JsonConvert.DeserializeObject<MyDoc>(jsonFileContent);
之后的 MyLine classes 列表工作正常,所有属性都保持正确的值,但通过 MyPolyline 列表 returns 正确数量的实体有一个问题,就是所有点的属性 X 和 Y 都为零。
json 文件本身没问题,我真的想不出任何办法让这个东西工作。以防万一 json 文件结构相当明显,但它看起来像这样:
{
"Lines": [
{
"StartPoint": {
"X": 1594.9640653785937,
"Y": 1490.1708760910014
},
"EndPoint": {
"X": 1455.6957137348581,
"Y": 1381.9184832054661
}
}
],
"Polylines": [
{
"Points": [
{
"X": 2155.1322935779444,
"Y": 2022.1540617522687
},
{
"X": 2291.3057975869833,
"Y": 1728.326139136384
}
]
}
]
}
所以问题是 - 有什么好的方法来处理这个问题吗?
如果您的变量名与 json 文件中的相同,您可以删除 json属性 属性。
class MyPoint2d
{
[JsonProperty("X")]
public double X { get; set; }
[JsonProperty("Y")]
public double Y { get; set; }
}
class MyPolyline
{
[JsonProperty("Points")]
public List<MyPoint2d> Points { get; set; }
}
class MyLine
{
[JsonProperty("StartPoint")]
public MyPoint2d StartPoint { get; set; }
[JsonProperty("EndPoint")]
public MyPoint2d EndPoint { get; set; }
}
class MyDoc
{
[JsonProperty("Lines")]
public List<MyLine> Lines { get; set; }
[JsonProperty("Polylines")]
public List<MyPolyline> Polylines { get; set; }
}