使用C#使用LINQ在json.net中创建树层次结构

用LINQ问答看了这个JSON中的Create tree层次结构,它很接近我的需要。但是我无法使用json.net生成如下所示的嵌套数组

Class.cs

    public class RootObject
    {
        public List<Attribute> test { get; set; }
    }
    public class Attribute
    {
        public string testKey { get; set; }
        public string start { get; set; }
        public string finish { get; set; }
        public string comment { get; set; }
        public string status { get; set; }
    }


    public class AttributeData
    {
        public Attribute AttributeDataOps()
        {
            ***Attribute DATA***
        }
    }

Program.cs

Attribute attribute = new Attribute();
AttributeData attributeData = new AttributeData();
string JSONresult = JsonConvert.SerializeObject(attribute);
string path = @"C:\file.json";

预期结果

{
    "tests" : [
        {
            "testKey" : "",
            "start" : "",
            "finish" : "",
            "comment" : "",
            "status" : ""
        }
    ]
}

当前结果

    {
        "testKey" : "",
        "start" : "",
        "finish" : "",
        "comment" : "",
        "status" : ""
    }
埃尔多

您期望有一个集合,但是传递了一个要序列化的对象。

var rootObject = new RootObject();
List<Attribute> attribute = new List<Attribute>();
rootObject.test = attribute;
string JSONresult = JsonConvert.SerializeObject(rootObject);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章