標籤:blog http io os ar for sp 資料 div
關於我工作中對Json處理的東西
第一:動態序列化類別
第二:時間格式處理
通常我們一個類裡 可能有十到更多的屬性,但是我們序列化通常只需要序列化其中的 三到五個這樣的話就會有多餘的資料
如果 我只想序列化 id 跟name如何處理
這是我找的網上的方法:
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(); } }}
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
關於Json.net 處理日期格式 看第一張圖就知道 Json.net 處理日期格式 這尼瑪根本就不是正常人類能看的懂得
你可能會發現Json.net 裡有關於處理日期的東西
但是這個時候你就會發現 再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);
你會發現 動態序列化 跟 時間處理的格式不能共存 這個問題 著實讓我小小的蛋疼了一把.....
解決方案:
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; } }}
一些關於Json.net的處理
本著對大家有協助的態度
關於Josn.net的 http://www.codeplex.com/
轉載自:http://www.cnblogs.com/yesehuaqingqian/p/3934532.html
轉載自——Json.net動態序列化以及對時間格式的處理