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

  

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章