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

时间:2016-07-19 10:06:06

标签: c# .net entity-framework linq

我有变体和子变体,这个子变体是那些将进行不同测试的实体。

现在我想从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
                                    {

                                    }
                                 )
                        }    
                    }
                }
            }

1 个答案:

答案 0 :(得分:1)

您需要为新的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