首页 > Web开发 > 详细

Invoke microsoft own System.Text.Json.JsonSerialize() method to serialize object

时间:2021-03-05 14:38:24      阅读:37      评论:0      收藏:0      [点我收藏+]

1.install-package system.text.json;

2.

using System.Text.Json;

using System.IO;

3.

 1 static void SystemTextJsonDemo()
 2         {
 3             var obj = new[]
 4             {
 5                 new
 6                 {
 7                     Id=1,
 8                     Name="Fred",
 9                     Age=10
10                 },
11                 new
12                 {
13                     Id=2,
14                     Name="Fred2",
15                     Age=20
16                 },
17                 new
18                 {
19                     Id=3,
20                     Name="Fred3",
21                     Age=30
22                 }
23             };
24             JsonSerializerOptions jso = new JsonSerializerOptions();
25             jso.WriteIndented = true;            
26             string jsonValue = JsonSerializer.Serialize(obj, typeof(object), jso);
27             File.WriteAllText("JsonText2.json", jsonValue);
28             Console.WriteLine(jsonValue);
29         }

技术分享图片

 

 

Please pay attention to the indented parts.Such as the JsonConvert.Serialize(obj,Formating.Indented);

技术分享图片

 

Invoke microsoft own System.Text.Json.JsonSerialize() method to serialize object

原文:https://www.cnblogs.com/Fred1987/p/14485400.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!