如何在嵌套json结构中合并重复的子记录



我有变体和子变体,这些子变体是将接受不同测试的实体。

现在,我想从json结构的嵌套子对象中删除重复的子变体。

Json结构:

[
  {
    "ParentVariantName": "Variant1",
    "TestList": [
      {
        "TestId": 100,
        "Version": 0,
        "SubVariantsList": []     
        ]
      },
      {
        "TestId": 101,
        "Version": 1.0,
        "SubVariantsList": [
          {
            "SourceSubVariantModel": {
              "Id": 69,
              "Name": "Abc",
              "DiffPerc": 100
            },
            "TargetSubVariantModel": {
              "Id": 70,
              "Name": "Pqr",
              "DiffPerc": 200
            }
          },
          {
            "SourceSubVariantModel": {
              "Id": 70,
              "Name": "Pqr",
              "SourceValue": 200
            },
            "TargetSubVariantModel": {
              "Id": 71,
              "Name": "Xyz",
              "TargetValue":300
            }
          }
        ]
      },
      {
        "TestId": 247,
        "Version": 3.0,
        "SubVariantsList": []
      },
      {
        "TestId": 248,
        "Version": 4.0,
        "SubVariantsList": []
      },
      {
        "TestId": 249,
        "Version": 5.0,
        "SubVariantsList": [] 
      },
      {
        "TestId": 250,
        "Version": 6.0,
        "SubVariantsList": []
      }
    ]
  }
]

例如:在TestId=101的上述json结构中:

Id 70 is repeating 2 times and so i want to merge duplicate records并且想要这样的输出:

预期输出:

[
  {
    "ParentVariantName": "Variant1",
    "TestList": [
      {
        "TestId": 100,
        "Version": 0,
        "SubVariantsList": []     
        ]
      },
      {
        "TestId": 101,
        "Version": 1.0,
        "SubVariantsList": [
          {
             "Id": 69,
             "Name": "Abc",
             "DiffPerc": 100
          },
          {
             "Id": 70,
             "Name": "Abc",
             "DiffPerc": 200
          },
          {
             "Id": 71,
             "Name": "Abc",
             "DiffPerc": 300
          },
        ]
      },
      {
        "TestId": 247,
        "Version": 3.0,
        "SubVariantsList": []
      },
      {
        "TestId": 248,
        "Version": 4.0,
        "SubVariantsList": []
      },
      {
        "TestId": 249,
        "Version": 5.0,
        "SubVariantsList": [] 
      },
      {
        "TestId": 250,
        "Version": 6.0,
        "SubVariantsList": []
      }
    ]
  }
]

这就是我正在尝试的:

 foreach (var item in data)
            {
                foreach (var testList in item.TestList)
                {
                    foreach (var cdList in testList.SubVariantsList)
                    {
                        if (list.Count == 0)
                        {
                             list.Add
                                 (
                                    new
                                    {
                                    }
                                 )
                        }    
                    }
                }
            }

如果尚未为新的SubVariantsList条目定义类,则需要为其定义类,并在为更新的SubVariantsList填充新列表时实例化它。

在下面的示例中,我将Model定义为这样一个类。它还定义了UpsertToList()方法,以帮助在没有重复的情况下填写列表。

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string json = @"
[
  {
    ""ParentVariantName"": ""Variant1"",
    ..... Removed to save space. Insert your input JSON here with " changed to "" ....
";
            var data = JsonConvert.DeserializeObject<dynamic>(json);
            foreach (var item in data)
                foreach (var test in item.TestList)
                {
                    var list = new List<Model>();
                    foreach (var pair in test.SubVariantsList)
                    {
                        Model.UpsertToList(list, pair.SourceSubVariantModel);
                        Model.UpsertToList(list, pair.TargetSubVariantModel);
                    }
                    test.SubVariantsList = Newtonsoft.Json.Linq.JToken.FromObject(list);
                }
            Console.WriteLine(JsonConvert.SerializeObject(data, Formatting.Indented));
            Console.ReadKey();
        }
    }
    class Model
    {
        public int Id;
        public string Name;
        public int DiffPerc;
        public static void UpsertToList(List<Model> list, dynamic sourceObject)
        {
            var item = list.FindLast(model => sourceObject.Id == model.Id);
            if (item == null)
                list.Add(new Model(sourceObject));
            else
                item.Update(sourceObject);
        }
        Model(dynamic sourceObject)
        {
            Id = sourceObject.Id;
            Update(sourceObject);
        }
        void Update(dynamic sourceObject)
        {
            Name = sourceObject.Name;
            var diffPerc = sourceObject["DiffPerc"];
            if (diffPerc == null)
                diffPerc = sourceObject["SourceValue"];
            if (diffPerc == null)
                diffPerc = sourceObject["TargetValue"];
            if (diffPerc != null)
                DiffPerc = diffPerc;
        }
    }
}

注意:我使用了Json.Net,所以您可能需要Install-Package Newtonsoft.Json

相关内容

  • 没有找到相关文章

最新更新