【发布时间】:2019-10-21 11:26:45
【问题描述】:
我在 C# 中有这个结构,我想创建可以成功转换为它的 JSON,以便我可以很容易地在 JS 逻辑中找到我的错误。
public class Tree
{
public Root Root { get; set; }
public Tree()
{}
}
public class Root : Node
{
public Root(int _id, int _fk, string _name, string _code, List<Node> _children, List<Item> _leaves)
: base(_id, _fk, _name, _code, _children, _leaves)
{
}
public Root()
{ }
}
public abstract class Node
{
public int? ID { get; set; }
public int? FK { get; set; }
public string Name { get; set; }
public string Code { get; set; }
public List<Node> children { get; set; }
public List<Item> leaves { get; set; }
public Node(int? _id, int? _fk, string _name, string _code, List<Node> _children, List<Item> _leaves)
{
ID = _id;
FK = _fk;
Name = _name;
Code = _code;
children = _children;
leaves = _leaves;
}
public Node ()
{}
}
public class Item
{
public string Code { get; set; }
public string Desc { get; set; }
public string Uom { get; set; }
public float? Measurements { get; set; }
public int? FK { get; set; }
public int? Tier { get; set; }
public bool IsADPresent { get; set; }
}
我试图在命令JsonConvert.DeserializeObject<Tree>(tree) 中输入的最基本的 JSON 是:
{
"Tree": {
"Root": {
"children": [],
"leaves": [],
"FK": 1,
"ID": 1,
"Name": " LOGISTICS",
"Code": "PT01"
}
}
}
但我仍然在 Tree 中得到 null;更不用说当 JSON 变得更加多毛时(这棵树最多可以有 5 个孙子)。
谢谢
更新:从 JSON 中删除 Tree 时出现异常:
Newtonsoft.Json.JsonSerializationException: 'Could not create an instance of type StatusUpdater.Models.NPoco.Node. Type is an interface or abstract class and cannot be instantiated. Path 'Root.children[0].children', line 1, position 33.'
【问题讨论】:
标签: c# json object tree json-deserialization