从 C# 模型创建一个嵌套的 json 对象

小马

这是我的json:

[
   {
      "ChangeFlowsFromParent":"false",
      "ChangeFlowsToParent":"true",
      "StreamType":"mainine",
      "streamName":"ArgOS_2_0",
      "Parent":"none",
      "Compliance":"Released",
      "children":[
         {
            "ChangeFlowsFromParent":"true",
            "ChangeFlowsToParent":"true",
            "StreamType":"Release",
            "streamName":"ArgOS_2_0_DHAL1",
            "Parent":"ArgOS_2_0",
            "Compliance":"Released",
            "children":[
               {
                  "ChangeFlowsFromParent":"false",
                  "ChangeFlowsToParent":"true",
                  "StreamType":"Release",
                  "streamName":"ArgOS_child_DHAL2",
                  "Parent":"ArgOS_2_0_DHAL1",
                  "Compliance":"Released",
                  "children":[
                     {
                        "ChangeFlowsFromParent":"false",
                        "ChangeFlowsToParent":"true",
                        "StreamType":"Release",
                        "streamName":"ArgOS_child_Gen2",
                        "Parent":"ArgOS_child_DHAL2",
                        "Compliance":"Released"
                     }
                  ]
               }
            ]
         },
         {
            "ChangeFlowsFromParent":"true",
            "ChangeFlowsToParent":"true",
            "StreamType":"Release",
            "streamName":"ArgOS_2_0_DHAL2",
            "Parent":"ArgOS_2_0",
            "Compliance":"NA"
         },
         {
            "ChangeFlowsFromParent":"false",
            "ChangeFlowsToParent":"false",
            "StreamType":"Release",
            "streamName":"ArgOS_2_0_DHAL3",
            "Parent":"ArgOS_2_0",
            "Compliance":"NA"
         }
      ]
   }
]

这是我的模型

public class TreeModel
{
    public string StreamName { get; set; }
    public string ParentName { get; set; }
    public string StreamType { get; set; }
    public bool ChangeFlowsFromParent { get; set; }
    public bool ChangeFlowsToParent { get; set; }
    public string Compliance { get; set; }
    public string Parent { get; set; }

}

所以我有一个模型形式的数据,我需要创建一个嵌套的 Json 结构,就像上面提到的那样。根据 parent = streamname,必须创建一个 children 标记,并且该模型项将作为 json 数组添加。

这个 JSON 用于我的树形图。这是如何实现的?

鲁福爵士

每当您必须序列化数据时,都会设计一个完全适合预期的导入/导出结构的类结构。不要关心应用程序的其余部分,只需关注输入/输出。

给定的 JSON 可以表示为

class TreeModelJson
{
    [JsonProperty("ChangeFlowsFromParent")]
    public string ChangeFlowsFromParent { get; set; }
    [JsonProperty("ChangeFlowsToParent")]
    public string ChangeFlowsToParent { get; set; }
    [JsonProperty("StreamType")]
    public string StreamType { get; set; }
    [JsonProperty("streamName")]
    public string StreamName { get; set; }
    [JsonProperty("Parent")]
    public string Parent { get; set; }
    [JsonProperty("Compliance")]
    public string Compliance { get; set; }
    [JsonProperty("children", NullValueHandling = NullValueHandling.Ignore)]
    public ICollection<TreeModelJson> Children { get; set; }
}

现在是时候编写从应用程序模型到 JSON 模型的映射器了

static ICollection<TreeModelJson> MapToTreeModelJsonCollection(ICollection<TreeModel> source)
{
    // map all items
    var allItems = source.Select(e => new TreeModelJson
    {
        ChangeFlowsFromParent = e.ChangeFlowsFromParent.ToString().ToLower(),
        ChangeFlowsToParent = e.ChangeFlowsToParent.ToString().ToLower(),
        Compliance = e.Compliance,
        Parent = e.Parent ?? "none",
        StreamName = e.StreamName,
        StreamType = e.StreamType,
    }).ToList();

    // build tree structure
    foreach (var item in allItems)
    {
        var children = allItems.Where(e => e.Parent == item.StreamName).ToList();
        if (children.Any())
        {
            item.Children = children;
        }
    }

    // return only root items
    return allItems.Where(e => e.Parent == "none").ToList();
}

现在是时候把这一切放在一起了

var source = new List<TreeModel>
{
    ... // populate some data
};
var output = MapToTreeModelJsonCollection(source);
var json = JsonConvert.SerializeObject(output,Formatting.Indented);

.net 小提琴上的完整示例

一些笔记

  • ParentJSON 中属性是多余的,因为如果对象是另一个对象的子对象,则此信息已经给出

  • JSON 知道boolean属性,最好将它们反序列化为 asboolean而不是 as string

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

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

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章