日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 人文社科 > 生活经验 >内容正文

生活经验

转载自——Json.net动态序列化以及对时间格式的处理

發(fā)布時(shí)間:2023/11/27 生活经验 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 转载自——Json.net动态序列化以及对时间格式的处理 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

關(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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。