1

我正在尝试使用 Json.NET 序列化和反序列化 jsTree 的树结构。

以下是类定义:

private class Metadata
    {
        [JsonProperty(PropertyName = "nodeType")]
        public NodeType NodeType;

        [JsonProperty(PropertyName = "typeDepth")]
        public int TypeDepth;
    }

    private class TreeNode
    {
        [JsonProperty(PropertyName = "data")]
        public String Title;

        [JsonIgnore]
        public NodeType NodeType;

        [JsonIgnore] 
        public int TypeDepth;

        [JsonProperty(PropertyName = "children", NullValueHandling = NullValueHandling.Ignore)]
        public List<TreeNode> Children;

        [JsonProperty(PropertyName = "metadata")]
        public Metadata Metadata
        {
            get
            {
                return new Metadata() {NodeType = NodeType, TypeDepth = TypeDepth};
            }

            set { 
                TypeDepth = value.TypeDepth;
                NodeType = value.NodeType;
            }
        }

        private ItemGroup _itemGroup;

        [JsonIgnore]
        public ItemGroup ItemGroup
        {
            get
            {
                if(this.NodeType != NodeType.ItemGroup)
                    throw new InvalidDataException();

                return _itemGroup;
            }

            set { _itemGroup = value; }
        }
    }

这是一些 JSON 示例:

[{
"data":"Strands",
"attr":{"class":""},
"state":"open",
"metadata":{
    "nodeType":3,
    "typeDepth":0},
"children":[{
    "data":"Math",
    "attr":{"class":"","rel":"itemGroup"},
    "state":"open",
    "metadata":{
        "nodeType":1,
        "typeDepth":0},
    "children":[{
        "data":"Subjects",
        "attr":{"class":""},
        "state":"open",
        "metadata":{"nodeType":3,"typeDepth":1},
        "children":[{
            "data":"Algebra 1",
            "attr":{"class":"","rel":"itemGroup"},
            "state":"open",
            "metadata":{
                "nodeType":1,
                "typeDepth":1},
            "children":[{
                "data":"Clusters",
                "attr":{"class":""},
                "state":"open",
                "metadata":{
                    "nodeType":3,
                    "typeDepth":2},
                "children":[{
                    "data":"Factoring",
                    "attr":{"rel":"itemGroup"},
                    "metadata":{
                        "nodeType":1,
                        "typeDepth":2}},
                    {"data":"Substitution",
                    "attr":{"class":"","rel":"itemGroup"},
                    "metadata":{"nodeType":1,"typeDepth":2}}]}]}]}]}]}]

我尝试像这样反序列化:List<TreeNode> tree = (List<TreeNode>)JsonConvert.DeserializeObject(form["treeJson"], typeof (List<TreeNode>));

树结构已正确反序列化,但没有一个节点具有元数据。

有人看到这里出了什么问题吗?

谢谢!

4

2 回答 2

1

当我将Metadata属性更改为

[JsonProperty(PropertyName = "metadata")]
public Metadata Metadata { get; set; }

它似乎工作正常

var tree = JsonConvert.DeserializeObject<List<TreeNode>>(jsonstring);

将其实施为的任何理由

[JsonProperty(PropertyName = "metadata")]
public Metadata Metadata
{
    get
    {
        return new Metadata() {NodeType = NodeType, TypeDepth = TypeDepth};
    }
    set { 
          TypeDepth = value.TypeDepth;
           NodeType = value.NodeType;
    }
}

?

于 2012-06-18T20:24:28.977 回答
0

类元数据是私有的。据我所知,这将阻止 Json 序列化程序访问它;因此,它会将属性值保留为空;

于 2012-06-18T18:55:59.533 回答