CSharp: read or write json using System.Text.Json in donet 7
/// <summary> /// 温度高低 /// geovindu /// </summary> public class HighLowTemps { /// <summary> /// 高 /// </summary> public int High { get; set; } /// <summary> /// 低 /// </summary> public int Low { get; set; } } /// <summary> /// 天气情况 /// geovindu /// </summary> public class WeatherForecast { /// <summary> /// 日期 /// </summary> public DateTimeOffset Date { get; set; } /// <summary> /// 摄氏温度 /// </summary> public int TemperatureCelsius { get; set; } /// <summary> /// 天气 /// </summary> public string? Summary { get; set; } /// <summary> /// 天气 摘要 /// </summary> public string? SummaryField; /// <summary> /// 时间 /// </summary> public IList<DateTimeOffset>? DatesAvailable { get; set; } /// <summary> /// 温度范围 /// </summary> public Dictionary<string, HighLowTemps>? TemperatureRanges { get; set; } /// <summary> /// 天气 相关关键描述 /// </summary> public string[]? SummaryWords { get; set; } } /// <summary> /// geovindu /// </summary> public class JsonBll { /// <summary> /// geovindu /// </summary> public JsonBll() { } /// <summary> /// /// </summary> /// <param name="json"></param> public JsonBll(string json) { } /// <summary> /// 序列化 Serialize /// </summary> /// <returns></returns> public string JsonSerialize() { var weatherForecast = new WeatherForecast { Date = DateTime.Parse("2019-08-01"), TemperatureCelsius = 25, Summary = "Hot", SummaryField = "Hot", DatesAvailable = new List<DateTimeOffset>() { DateTime.Parse("2019-08-01"), DateTime.Parse("2019-08-02") }, TemperatureRanges = new Dictionary<string, HighLowTemps> { ["Cold"] = new HighLowTemps { High = 20, Low = -10 }, ["Hot"] = new HighLowTemps { High = 60, Low = 20 } }, SummaryWords = new[] { "Cool", "Windy", "Humid" } }; var options = new JsonSerializerOptions { WriteIndented = true }; string jsonString = JsonSerializer.Serialize(weatherForecast, options); //JsonSerializer.Deserialize return jsonString; } /// <summary> /// 序列化 /// </summary> /// <param name="weatherForecast"></param> /// <returns></returns> public string JsonSerialize(WeatherForecast weatherForecast) { //var weatherForecast = new WeatherForecast //{ // Date = DateTime.Parse("2019-08-01"), // TemperatureCelsius = 25, // Summary = "Hot", // SummaryField = "Hot", // DatesAvailable = new List<DateTimeOffset>() // { DateTime.Parse("2019-08-01"), DateTime.Parse("2019-08-02") }, // TemperatureRanges = new Dictionary<string, HighLowTemps> // { // ["Cold"] = new HighLowTemps { High = 20, Low = -10 }, // ["Hot"] = new HighLowTemps { High = 60, Low = 20 } // }, // SummaryWords = new[] { "Cool", "Windy", "Humid" } //}; var options = new JsonSerializerOptions { WriteIndented = true }; string jsonString = JsonSerializer.Serialize(weatherForecast, options); //JsonSerializer.Deserialize return jsonString; } /// <summary> /// 反序列化 Deserialize /// </summary> /// <param name="strJson"></param> /// <returns></returns> public WeatherForecast JsonDeserialize(string strJson) { WeatherForecast weatherForecast = null; try { if(!string.IsNullOrEmpty(strJson)) { weatherForecast = JsonSerializer.Deserialize<WeatherForecast>(strJson)!; } } catch(Exception ex) { ex.Message.ToString(); } return weatherForecast; } }
调用:
JsonBll jsonBll = new JsonBll(); string str = jsonBll.JsonSerialize(); Console.WriteLine(str); WeatherForecast weatherForecast=jsonBll.JsonDeserialize(str); Console.WriteLine("天气:"+weatherForecast.Summary);
输出:
{ "Date": "2019-08-01T00:00:00+08:00", "TemperatureCelsius": 25, "Summary": "Hot", "DatesAvailable": [ "2019-08-01T00:00:00+08:00", "2019-08-02T00:00:00+08:00" ], "TemperatureRanges": { "Cold": { "High": 20, "Low": -10 }, "Hot": { "High": 60, "Low": 20 } }, "SummaryWords": [ "Cool", "Windy", "Humid" ] } 天气:Hot
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)