CSharp: read or write json using System.Text.Json in donet 7

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/// <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;
       }
 
 
       
   } 

 

调用:

1
2
3
4
5
6
7
JsonBll jsonBll = new JsonBll();
string str = jsonBll.JsonSerialize();  
Console.WriteLine(str);
 
 
WeatherForecast weatherForecast=jsonBll.JsonDeserialize(str);
Console.WriteLine("天气:"+weatherForecast.Summary);

  

输出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
{
  "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

  

 

posted @   ®Geovin Du Dream Park™  阅读(18)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· Qt个人项目总结 —— MySQL数据库查询与断言
历史上的今天:
2015-03-21 sql:sql server,MySQL,PostgreSQL的表,视图,存储过程结构查询
2012-03-21 webform TextBox以一条横线显示 ---兼容各主流瀏覽器 .
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示