转载自——Json.net动态序列化以及对时间格式的处理
關(guān)于我工作中對(duì)Json處理的東西
第一:動(dòng)態(tài)序列化類??
第二:時(shí)間格式處理
通常我們一個(gè)類里 可能有十到更多的屬性,但是我們序列化通常只需要序列化其中的 三到五個(gè)這樣的話就會(huì)有多余的數(shù)據(jù)
?
?
?
如果 我只想序列化?? id 跟name如何處理
這是我找的網(wǎng)上的方法:
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.Linq;namespace CYP.New.WCF.Common.Common
{public class LimitPropsContractResolver : DefaultContractResolver{private string[] props = null;public LimitPropsContractResolver(string[] props){this.props = props;}protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization){IList<JsonProperty> list = base.CreateProperties(type, memberSerialization);//只保留清單有列出的屬性return list.Where(p => props.Contains(p.PropertyName)).ToList();}}
} ?
?
?
?
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
關(guān)于Json.net 處理日期格式????? 看第一張圖就知道? Json.net 處理日期格式 這尼瑪根本就不是正常人類能看的懂得
?
你可能會(huì)發(fā)現(xiàn)Json.net 里有關(guān)于處理日期的東西
?
?
?
但是這個(gè)時(shí)候你就會(huì)發(fā)現(xiàn)?? 再Json.net的重載里邊
public static string SerializeObject(object value);public static string SerializeObject(object value, Formatting formatting);public static string SerializeObject(object value, JsonSerializerSettings settings);public static string SerializeObject(object value, params JsonConverter[] converters);public static string SerializeObject(object value, Formatting formatting, JsonSerializerSettings settings);public static string SerializeObject(object value, Formatting formatting, params JsonConverter[] converters);
你會(huì)發(fā)現(xiàn)?? 動(dòng)態(tài)序列化 跟? 時(shí)間處理的格式不能共存??? 這個(gè)問題 著實(shí)讓我小小的蛋疼了一把.....
解決方案:
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.Linq;namespace CYP.New.WCF.Common.Common
{public class LimitPropsContractResolver : DefaultContractResolver{private string[] props = null;public LimitPropsContractResolver(string[] props){this.props = props;}protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization){IList<JsonProperty> list = base.CreateProperties(type, memberSerialization);IsoDateTimeConverter iso = new IsoDateTimeConverter() { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" };IList<JsonProperty> listWithConver = new List<JsonProperty>();foreach (var item in list){if (props.Contains(item.PropertyName)){if (item.PropertyType.ToString().Contains("System.DateTime")){item.Converter = iso;}listWithConver.Add(item);}}return listWithConver;}}
} ?
?
一些關(guān)于Json.net的處理??
本著對(duì)大家有幫助的態(tài)度????
關(guān)于Josn.net的下載地址 http://www.codeplex.com/ ? ?
?
?
轉(zhuǎn)載自:http://www.cnblogs.com/yesehuaqingqian/p/3934532.html
轉(zhuǎn)載于:https://www.cnblogs.com/ShaYeBlog/p/4050285.html
總結(jié)
以上是生活随笔為你收集整理的转载自——Json.net动态序列化以及对时间格式的处理的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Centos安装GD库
- 下一篇: C语言中将0到1000的浮点数用强制指针