JSON使用DateTimeZoneHandling序列化时时区与日期
来源:互联网 发布:php 文件上传原理 编辑:程序博客网 时间:2024/06/05 20:32
1.先创建一个Flight类
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace JSONDemo{ public class Flight { public string Destination { get; set; } public DateTime DepartureDate { get; set; } public DateTime DepartureDateUtc { get; set; } public DateTime DepartureLocal { get; set; } public TimeSpan Duration { get; set; } }}
2.使用DateTimeZoneHandling序列化日期,指定是否显示时区
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Data;using GongHuiNewtonsoft.Json;namespace JSONDemo{ class Program { static void Main(string[] args) { Flight flight = new Flight { Destination = "Paris", DepartureDate = new DateTime(2015, 12, 11, 0, 0, 0, DateTimeKind.Unspecified), DepartureDateUtc = new DateTime(2015, 12, 11, 0, 0, 0, DateTimeKind.Utc), DepartureLocal = new DateTime(2015, 12, 11, 0, 0, 0, DateTimeKind.Local), Duration = TimeSpan.FromHours(11.267) }; Console.WriteLine("---------只对本地时间显示时区-------------"); string roundtripTimeZone = JsonConvert.SerializeObject(flight, Formatting.Indented, new JsonSerializerSettings { DateTimeZoneHandling = DateTimeZoneHandling.RoundtripKind }); Console.WriteLine(roundtripTimeZone); Console.WriteLine("---------对所有时间显示时区---------------"); string localTimeZone = JsonConvert.SerializeObject(flight, Formatting.Indented, new JsonSerializerSettings { DateTimeZoneHandling = DateTimeZoneHandling.Local }); Console.WriteLine(localTimeZone); Console.WriteLine("----------------不显示时区---------------"); string unspecifiedTimeZone = JsonConvert.SerializeObject(flight, Formatting.Indented, new JsonSerializerSettings { DateTimeZoneHandling = DateTimeZoneHandling.Unspecified }); Console.WriteLine(unspecifiedTimeZone); } }}
3.运行后的结果
JSON源代码下载地址:http://download.csdn.net/detail/lovegonghui/9342751
0 0
- JSON使用DateTimeZoneHandling序列化时时区与日期
- JSON使用DateFormatHandling 序列化与反序列化日期设置
- 序列化与json
- 解决JSON序列化日期格式问题
- Newtonsoft.Json.JsonConvert 序列化日期时间
- java json序列化日期类型
- Newtonsoft.Json.dll使用 .net json序列化与返序列化
- json-lib序列化与反序列化日期格式的数据
- JSON使用DefaultSettings序列化与反序列化
- 使用Json.net序列化和反序列化自定义日期
- JSON解析与序列化
- JSON解析与序列化
- JSON解析与序列化
- JSON--解析与序列化
- json与pickle序列化
- json与序列化pickling
- 使用ObjectMapper 序列化Json
- VS2008下使用Newtonsoft.Json对JSON的序列化与反序列化
- 歲月,請溫柔以待
- curl openstack
- 异常处理的最佳实践[转载]
- java并发编程学习之interrupt相关方法学习(二)
- Linux:bootloader概述
- JSON使用DateTimeZoneHandling序列化时时区与日期
- mybatis 简单分页操作
- java自定义注解简单小例子
- 基于rails的schedule网站开发(3):如何使用MySql开发(?)
- 学习OpenCV范例(十一)——图像的腐蚀与膨胀
- gem install nokogiri -v '1.6.6.2' 出错
- centos 建立本地yum源
- 一个整型数组中,每个元素两两出现,其中一个元素只出现一次,请找出来
- 使用script.src实现跨域