Home > OS >  combine list objects to json object no duplicates
combine list objects to json object no duplicates

Time:10-14

{ "objtest": [ "2022-09-30T00:00:00", "2022-06-30T00:00:00", "2022-03-31T00:00:00", "2021-12-31T00:00:00", "2021-09-30T00:00:00", "2021-06-30T00:00:00", "2021-03-31T00:00:00", "2020-12-31T00:00:00", "2020-09-30T00:00:00", "2020-06-30T00:00:00", "2020-03-31T00:00:00", "2019-12-31T00:00:00", "2019-09-30T00:00:00", "2019-06-30T00:00:00", "2019-03-31T00:00:00", "2018-12-31T00:00:00", "2018-09-30T00:00:00", "2018-06-30T00:00:00", "2018-03-31T00:00:00", "2017-12-31T00:00:00", "2017-09-30T00:00:00", "2017-06-30T00:00:00", "2017-03-31T00:00:00", "2016-12-30T00:00:00" ], "objfinaltest": [ true, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, false ] }

c#

{
    public List<DateTime> objtest { get; set; }
    public List<bool> objfinaltest { get; set; }
}

Below is my json return

   var display = new Root()
                        {
                            objtest = objtest,
                            objfinaltest = objfinaltest

                        };

The final results I need is

{objtest: "2022-09-30T00:00:00", objfinaltest : true}, {objtest: "2022-06-30T00:00:00", objfinaltest : false} ect... at least close to the format.

CodePudding user response:

Here is how to combine into one object

 var results = objtest.Select((x, i) => new { objtest = x, objfinaltest = objtest[i] })
                .GroupBy(x => x.objtest)
                .Select(x => x.First())
                .ToList();

CodePudding user response:

you can try to use Linq Zip, and you don't need your Root class. You can create your custom class instead of anonymous , if you need

   using Newtonsoft.Json;

   var jsonParsed=JObject.Parse(json);
  
    var result = jsonParsed["objtest"].ToObject<List<DateTime>>()
    .Zip(jsonParsed["objfinaltest"].ToObject<List<bool>>(), 
    (first, second) => new { objtest=first, objfinaltest= second})
    .ToList();
  • Related