【问题标题】:Create tree hierarchy in json.net with LINQ using c#使用 c# 在 json.net 中使用 LINQ 创建树层次结构
【发布时间】:2018-10-18 08:45:45
【问题描述】:

我查看了这个Create tree hierarchy in JSON with LINQ 问题和答案,它与我所需要的很接近。但是我无法生成嵌套数组,如下所示,因为我使用的是 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" : ""
    }

【问题讨论】:

    标签: c# linq json.net


    【解决方案1】:

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

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

    【讨论】:

    • 列表 部分无法显示。我需要为此创建一个对象吗?
    • 你需要给它增值
    猜你喜欢
    • 2011-03-10
    • 1970-01-01
    • 1970-01-01
    • 2017-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多