当前位置:
首页 > Python基础教程 >
-
C#教程之C# Json.Net解析实例(2)
407 this.txtJson.Text += "\r\n";
408 string jsonMsDate = JsonConvert.SerializeObject(mayanEndOfTheWorld, new JsonSerializerSettings
409 {
410 DateFormatHandling = DateFormatHandling.MicrosoftDateFormat
411 });
412
413 this.txtJson.Text += (jsonMsDate);
414 // "\/Date(1356044400000+0100)\/"
415 this.txtJson.Text += "\r\n";
416 string json = JsonConvert.SerializeObject(mayanEndOfTheWorld, new JsonSerializerSettings
417 {
418 DateFormatString = "yyyy-MM-dd",
419 Formatting = Formatting.Indented
420 });
421 this.txtJson.Text += json;
422 }
423
424 private void btnSerializeDateZone_Click(object sender, EventArgs e)
425 {
426 Flight flight = new Flight
427 {
428 Destination = "Dubai",
429 DepartureDate = new DateTime(2013, 1, 21, 0, 0, 0, DateTimeKind.Unspecified),
430 DepartureDateUtc = new DateTime(2013, 1, 21, 0, 0, 0, DateTimeKind.Utc),
431 DepartureDateLocal = new DateTime(2013, 1, 21, 0, 0, 0, DateTimeKind.Local),
432 Duration = TimeSpan.FromHours(5.5)
433 };
434
435 string jsonWithRoundtripTimeZone = JsonConvert.SerializeObject(flight, Formatting.Indented, new JsonSerializerSettings
436 {
437 DateTimeZoneHandling = DateTimeZoneHandling.RoundtripKind
438 });
439
440 this.txtJson.Text=(jsonWithRoundtripTimeZone);
441 this.txtJson.Text += "\r\n";
442 string jsonWithLocalTimeZone = JsonConvert.SerializeObject(flight, Formatting.Indented, new JsonSerializerSettings
443 {
444 DateTimeZoneHandling = DateTimeZoneHandling.Local
445 });
446
447 this.txtJson.Text+=(jsonWithLocalTimeZone);
448 this.txtJson.Text += "\r\n";
449
450 string jsonWithUtcTimeZone = JsonConvert.SerializeObject(flight, Formatting.Indented, new JsonSerializerSettings
451 {
452 DateTimeZoneHandling = DateTimeZoneHandling.Utc
453 });
454
455 this.txtJson.Text += (jsonWithUtcTimeZone);
456 this.txtJson.Text += "\r\n";
457
458 string jsonWithUnspecifiedTimeZone = JsonConvert.SerializeObject(flight, Formatting.Indented, new JsonSerializerSettings
459 {
460 DateTimeZoneHandling = DateTimeZoneHandling.Unspecified
461 });
462
463 this.txtJson.Text += (jsonWithUnspecifiedTimeZone);
464
465 }
466
467 private void btnSerializeDataContract_Click(object sender, EventArgs e)
468 {
469 CFile file = new CFile
470 {
471 Id = Guid.NewGuid(),
472 Name = "ImportantLegalDocuments.docx",
473 Size = 50 * 1024
474 };
475
476 string json = JsonConvert.SerializeObject(file, Formatting.Indented);
477
478 this.txtJson.Text=(json);
479 }
480
481 /// <summary>
482 /// 序列化默认设置
483 /// </summary>
484 /// <param name="sender"></param>
485 /// <param name="e"></param>
486 private void btnSerializeDefaultSetting_Click(object sender, EventArgs e)
487 {
488 // settings will automatically be used by JsonConvert.SerializeObject/DeserializeObject
489 JsonConvert.DefaultSettings = () => new JsonSerializerSettings
490 {
491 Formatting = Formatting.Indented,
492 ContractResolver = new CamelCasePropertyNamesContractResolver()
493 };
494
495 Person s = new Person()
496 {
497 Name = "Eric",
498 Birthday = new DateTime(1980, 4, 20, 0, 0, 0, DateTimeKind.Utc),
499 Gender = "男",
500 Love = "Web Dude"
501 };
502
503 string json = JsonConvert.SerializeObject(s);
504 this.txtJson.Text = json;
505 }
506
507 /// <summary>
508 /// 序列化Immutable
509 /// </summary>
510 /// <param name="sender"></param>
511 /// <param name="e"></param>
512 private void btnSerializeImmutable_Click(object sender, EventArgs e)
513 {
514 //ImmutableList<string> l = ImmutableList.CreateRange(new List<string>
515 // {
516 // "One",
517 // "II",
518 // "3"
519 // });
520
521 //string json = JsonConvert.SerializeObject(l, Formatting.Indented);
522
523 }
524
525 /// <summary>
526 /// 序列化JsonProperty
527 /// </summary>
528 /// <param name="sender"></param>
529 /// <param name="e"></param>
530 private void btnSerializeJsonProperty_Click(object sender, EventArgs e)
531 {
532 Videogame starcraft = new Videogame
533 {
534 Name = "Starcraft",
535 ReleaseDate = new DateTime(1998, 1, 1)
536 };
537
538 string json = JsonConvert.SerializeObject(starcraft, Formatting.Indented);
539
540 this.txtJson.Text = json;
541 }
542
543 /// <summary>
544 /// 序列化排序,值越小,月靠前,默认是0
545 /// </summary>
546 /// <param name="sender"></param>
547 /// <param name="e"></param>
548 private void btnSerializeOrder_Click(object sender, EventArgs e)
549 {
550 Account0 account = new Account0
551 {
552 FullName = "Aaron Account",
553 EmailAddress = "aaron@example.com",
554 Deleted = true,
555 DeletedDate = new DateTime(2013, 1, 25),
556 UpdatedDate = new DateTime(2013, 1, 25),
557 CreatedDate = new DateTime(2010, 10, 1)
558 };
559
560 string json = JsonConvert.SerializeObject(account, Formatting.Indented);
561
562 this.txtJson.Text=(json);
563
564 }
565
566 private void btnSerializeJsonConstructor_Click(object sender, EventArgs e)
567 {
568 string json = @"{
569 ""UserName"": ""domain\\username"",
570 ""Enabled"": true
571 }";
572
573 User user = JsonConvert.DeserializeObject<User>(json);
574
575 this.txtJson.Text=(user.UserName);
576
577 }
578
579 private void btnSerializeJsonIgnore_Click(object sender, EventArgs e)
580 {
581 Account1 account = new Account1
582 {
583 FullName = "Joe User",
584 EmailAddress = "joe@example.com",
585 PasswordHash = "VHdlZXQgJ1F1aWNrc2lsdmVyJyB0byBASmFtZXNOSw=="
586 };
587
588 string json = JsonConvert.SerializeObject(account);
589 this.txtJson.Text = json;
590 }
591
592 /// <summary>
593 /// 其他功能
594 /// </summary>
595 /// <param name="sender"></param>
596 /// <param name="e"></param>
597 private void btnOtherFunction_Click(object sender, EventArgs e)
598 {
599 JsonForm1 jsonOther = new JsonForm1();
600 jsonOther.ShowDialog();
601 }
602 }
其他功能
具体如下图所示【其他功能】:
其他功能代码如下
1 public partial class JsonForm1 : Form 2 { 3 public JsonForm1() 4 { 5 InitializeComponent(); 6 } 7 8 /// <summary> 9 /// 手动创建Json 10 /// </summary> 11 /// <param name="sender"></param> 12 /// <param name="e"></param> 13 private void btnCreateJsonManually_Click(object sender, EventArgs e) 14 { 15 JArray array = new JArray(); 16 array.Add("Manual text"); 17 array.Add(new DateTime(2000, 5, 23)); 18 19 JObject o = new JObject(); 20 o["MyArray"] = array; 21 22 string json = o.ToString(); 23 this.txtJson.Text = json; 24 25 } 26 27 /// <summary> 28 /// 列表创建Json 29 /// </summary> 30 /// <param name="sender"></param> 31 /// <param name="e"></param> 32 private void btnCollectionJson_Click(object sender, EventArgs e) 33 { 34 JObject o = new JObject() 35 { 36 { "Cpu", "Intel" }, 37 { "Memory", 32 }, 38 { 39 "Drives", new JArray 40 { 41 "DVD", 42 "SSD" 43 } 44 } 45 }; 46 47 this.txtJson.Text = o.ToString(); 48 } 49 50 private void btnCreateJsonByDynamic_Click(object sender, EventArgs e) 51 { 52 dynamic product = new JObject(); 53 product.ProductName = "Elbow Grease"; 54 product.Enabled = true; 55 product.Price = 4.90m; 56 product.StockCount = 9000; 57 product.StockValue = 44100; 58 product.Tags = new JArray("Real", "OnSale"); 59 this.txtJson.Text = product.ToString(); 60 61 } 62 63 /// <summary> 64 /// 从JTokenWriter创建Json 65 /// </summary> 66 /// <param name="sender"></param> 67 /// <param name="e"></param> 68 private void btnJTokenWriter_Click(object sender, EventArgs e) 69 { 70 JTokenWriter writer = new JTokenWriter(); 71 writer.WriteStartObject(); 72 writer.WritePropertyName("name1"); 73 writer.WriteValue("value1"); 74 writer.WritePropertyName("name2"); 75 writer.WriteStartArray(); 76 writer.WriteValue(1); 77 writer.WriteValue(2); 78 writer.WriteEndArray(); 79 writer.WriteEndObject(); 80 81 JObject o = (JObject)writer.Token; 82 83 this.txtJson.Text = o.ToString(); 84 } 85 86 /// <summary> 87 /// 从对象创建Json 88 /// </summary> 89 /// <param name="sender"></param> 90 /// <param name="e"></param> 91 private void btnCreateJsonFromObject_Click(object sender, EventArgs e) 92 { 93 JValue i = (JValue)JToken.FromObject(12345); 94 95 Console.WriteLine(i.Type); 96 // Integer 97 Console.WriteLine(i.ToString()); 98 // 12345 99 100 JValue s = (JValue)JToken.FromObject("A string"); 101 102 Console.WriteLine(s.Type); 103 // String 104 Console.WriteLine(s.ToString()); 105 // A string 106 107 Computer computer = new Computer 108 { 109 Cpu = "Intel", 110 Memory = 32, 111 Drives = new List<string> 112 { 113 "DVD", 114 "SSD" 115 } 116 }; 117 118 JObject o = (JObject)JToken.FromObject(computer); 119 120 Console.WriteLine(o.ToString()); 121 // { 122 // "Cpu": "Intel", 123 // "Memory": 32, 124 // "Drives": [ 125 // "DVD", 126 // "SSD" 127 // ] 128 // } 129 130 JArray a = (JArray)JToken.FromObject(computer.Drives); 131 132 Console.WriteLine(a.ToString()); 133 // [ 134 // "DVD", 135 // "SSD" 136 // ] 137 } 138 139 /// <summary> 140 /// 从匿名对象创建 141 /// </summary> 142 /// <param name="sender"></param> 143 /// <param name="e"></param> 144 private void btnCreateFromAnaymous_Click(object sender, EventArgs e) 145 { 146 List<Post> posts = new List<Post> 147 { 148 new Post 149 { 150 Title = "Episode VII", 151 Description = "Episode VII production", 152 Categories = new List<string> 153 { 154 "episode-vii", 155 "movie" 156 }, 157 Link = "episode-vii-production.aspx" 158 } 159 }; 160 161 JObject o = JObject.FromObject(new 162 { 163 channel = new 164 { 165 title = "Star Wars", 166 link = "http://www.starwars.com", 167 description = "Star Wars blog.", 168 item = 169 from p in posts 170 orderby p.Title 171 select new 172 { 173 title = p.Title, 174 description = p.Description, 175 link = p.Link, 176 category = p.Categories 177 } 178 } 179 }); 180 181 this.txtJson.Text=o.ToString(); 182 183 } 184 185 /// <summary> 186 /// Parse 187 /// </summary> 188 /// <param name="sender"></param> 189 /// <param name="e"></param> 190 private void btnJArrayParse_Click(object sender, EventArgs e) 191 { 192 string json = @"[ 193 'Small', 194 'Medium', 195 'Large' 196 ]"; 197 198 JArray a = JArray.Parse(json); 199 200 this.txtJson.Text = a.ToString(); 201 this.txtJson.Text += "\r\n"; 202 203 json = @"{ 204 CPU: 'Intel', 205 Drives: [ 206 'DVD read/writer', 207 '500 gigabyte hard drive' 208 ] 209 }"; 210 211 JObject o = JObject.Parse(json); 212 213 this.txtJson.Text += o.ToString(); 214 215 JToken t1 = JToken.Parse("{}"); 216 217 Console.WriteLine(t1.Type); 218 // Object 219 220 JToken t2 = JToken.Parse("[]"); 221 222 Console.WriteLine(t2.Type); 223 // Array 224 225 JToken t3 = JToken.Parse("null"); 226 227 Console.WriteLine(t3.Type); 228 // Null 229 230 JToken t4 = JToken.Parse(@"'A string!'"); 231 232 Console.WriteLine(t4.Type); 233 // String 234 } 235 236
栏目列表
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
SQL SERVER中递归
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比
一款纯 JS 实现的轻量化图片编辑器
关于开发 VS Code 插件遇到的 workbench.scm.
前端设计模式——观察者模式
前端设计模式——中介者模式
创建型-原型模式