CSharp · Console
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
public class Program
{
public static void Main()
{
// sales data
var sellerId = "157747190185796800";
var listToReturn = new List<SalesDataRow>
{
new SalesDataRow { DateFromStr = "2020-08-25", Sales = 195000, Commission = 25350 },
new SalesDataRow { DateFromStr = "2020-08-26", Sales = 70500, Commission = 9165 },
new SalesDataRow { DateFromStr = "2020-08-27", Sales = 51000, Commission = 6630 },
};
// shape the data into the structure we want for the JSON
var resultToReturn = new
{
sellerId,
data = listToReturn.ToDictionary (
item => item.DateFromStr,
item => new
{
sales = item.Sales,
commission = item.Commission
}
)
};
// serialize
var json = JsonConvert.SerializeObject(resultToReturn, Formatting.Indented);
Console.WriteLine(json);
}
}
class SalesDataRow
{
public string DateFromStr { get; set; }
public int Sales { get; set; }
public int Commission { get; set; }
}