1 string fileName = "WeatherForecast.json";
2 string jsonString = JsonSerializer.Serialize<WeatherForecast>(weatherForecast);
3 File.WriteAllText(fileName, jsonString);
1using System;
2using System.Text.Json;
3
4namespace SerializeWithGenericParameter
5{
6 public class WeatherForecast
7 {
8 public DateTimeOffset Date { get; set; }
9 public int TemperatureCelsius { get; set; }
10 public string Summary { get; set; }
11 }
12
13 public class Program
14 {
15 public static void Main()
16 {
17 var weatherForecast = new WeatherForecast
18 {
19 Date = DateTime.Parse("2019-08-01"),
20 TemperatureCelsius = 25,
21 Summary = "Hot"
22 };
23
24 string jsonString = JsonSerializer.Serialize<WeatherForecast>(weatherForecast);
25
26 Console.WriteLine(jsonString);
27 }
28 }
29}