I’m trying to create a JSON array that contains list of weekdays.
I’m testing this in a console app but it keeps giving me an error i.e. exited with code 0.
Here’s what I’m doing:
using Newtonsoft.Json;
var days = new List<DayOfWeek>();
days.Add(DayOfWeek.Monday);
days.Add(DayOfWeek.Wednesday);
var json = JsonConvert.SerializeObject(days);
Console.WriteLine(json);
What am I doing wrong?
>Solution :
First, use System.Text.Json, as it’s more up to date.
Second, you can do what you want with a Linq expression…
var days = JsonSerializer.Serialize(Enum.GetValues(typeof(DayOfWeek))
.Cast<DayOfWeek>().ToList());
Not actually sure if you need the Cast<> in there, but this certainly works.