简体   繁体   中英

How to get a nested json result after grouping a list?

I want to convert following list to JSON result by grouping the list by Category, Merchants and Vouchers (JSON result also shown below). I tried to solve this issue using LINQ queries, but could not do it.

Need to convert following List to JSON result.

    var list = new List<Data>() { 
    new Data { categoryCode = "1", categoryName = "A", merchantId = "1", mechantName = "Shehan", voucherId = "200", voucherDescription = "Voucher - 200"}, 
    new Data { categoryCode = "1", categoryName = "A", merchantId = "1", mechantName = "Shehan", voucherId = "300", voucherDescription = "Voucher - 300"}, 
    new Data { categoryCode = "1", categoryName = "A", merchantId = "2", mechantName = "Ashen", voucherId = "200", voucherDescription = "Voucher - 200"},
    new Data { categoryCode = "2", categoryName = "B", merchantId = "1", mechantName = "Shehan", voucherId = "200", voucherDescription = "Voucher - 200"},
    new Data { categoryCode = "2", categoryName = "B", merchantId = "1", mechantName = "Shehan", voucherId = "300", voucherDescription = "Voucher - 300"},
    new Data { categoryCode = "2", categoryName = "B", merchantId = "2", mechantName = "Ashen", voucherId = "200", voucherDescription = "Voucher - 200"},
    new Data { categoryCode = "2", categoryName = "B", merchantId = "2", mechantName = "Ashen", voucherId = "300", voucherDescription = "Voucher - 300"},
};

This is the Data Model used in the list.

class Data
{
    public string categoryCode { get; set; }
    public string categoryName { get; set; }
    public string merchantId { get; set; }
    public string mechantName { get; set; }
    public string voucherId { get; set; }
    public string voucherDescription { get; set; }
}

Need to get the following JSON Result from the given list.

"Data": [
   {
      "categoryCode":"1",
      "categoryName":"A",
      "merchants":[
         {
            "merchantId":"1",
            "merchantName":"Shehan",
            "vouchers":[
               {
                  "Id":"200",
                  "description":"Voucher - 200"
               },
               {
                  "Id":"300",
                  "description":"Voucher - 300"
               }
            ]
         },
         {
            "merchantId":"2",
            "merchantName":"Ashen",
            "vouchers":[
               {
                  "Id":"200",
                  "description":"Voucher - 200"
               }
            ]
         }
      ]
   },
   {
      "categoryCode":"2",
      "categoryName":"B",
      "merchants":[
         {
            "merchantId":"1",
            "merchantName":"Shehan",
            "vouchers":[
               {
                  "Id":"200",
                  "description":"Voucher - 200"
               },
               {
                  "Id":"300",
                  "description":"Voucher - 300"
               }
            ]
         },
         {
            "merchantId":"2",
            "merchantName":"Ashen",
            "vouchers":[
               {
                  "Id":"200",
                  "description":"Voucher - 200"
               },
               {
                  "Id":"300",
                  "description":"Voucher - 300"
               }
            ]
         }
      ]
   }
]

Really appreciate if you could help on this. :)

I think you can do that by this way:

 var result = list.GroupBy(x => new { x.categoryCode, x.categoryName })
                .Select(x => new
                {
                    x.Key.categoryCode,
                    x.Key.categoryName,
                    Merchants = x.ToList().GroupBy(g => new { g.merchantId, g.mechantName })
                        .Select(v => new
                        {
                            v.Key.merchantId,
                            v.Key.mechantName,
                            vouchers = v.Select(s => new { Id = s.voucherId, description = s.voucherDescription })
                                .ToList()
                        })
                });

            var json = JsonConvert.SerializeObject(result);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM