我在C#中具有此结构,我想创建可以成功转换为JSON的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
,因为Tree
是目标类型,并且不需要包含在JSON中。
{
"Root": {
"children": [],
"leaves": [],
"FK": 1,
"ID": 1,
"Name": " LOGISTICS",
"Code": "PT01"
}
}
在json中省略Tree
属性。
为了进行测试,您可以将树实例序列化为json以查看其外观。大概是这样{"Root":{"children":[],"leaves":[],"FK":1,"ID":1,"Name":" LOGISTICS","Code":"PT01"}}