转载自——Json.net动态序列化以及对时间格式的处理
关于我工作中对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动态序列化以及对时间格式的处理的更多相关文章
- Newtonsoft.Json 序列化和反序列化 时间格式【转】
1.JSON序列化 string JsonStr= JsonConvert.SerializeObject(Entity); eg: A a=new A(); a.Name="Elain ...
- Newtonsoft.Json 序列化和反序列化 时间格式
From : http://www.cnblogs.com/litian/p/3870975.html 1.JSON序列化 string JsonStr= JsonConvert.SerializeO ...
- Newtonsoft.Json 序列化和反序列化 时间格式 [转]
1.JSON序列化 string JsonStr= JsonConvert.SerializeObject(Entity); eg: A a=new A(); a.Name="Elain ...
- [转]Newtonsoft.Json 序列化和反序列化 时间格式
本文转自:http://www.cnblogs.com/litian/p/3870975.html 1.JSON序列化 string JsonStr= JsonConvert.SerializeObj ...
- C#中Newtonsoft.Json 序列化和反序列化 时间格式
步骤 引用 using Newtonsoft.Json; using Newtonsoft.Json.Converters; 格式配置 IsoDateTimeConverter timeFormat ...
- 使用Newtonsoft.Json.dll(JSON.NET)动态解析JSON、.net 的json的序列化与反序列化(一)
在开发中,我非常喜欢动态语言和匿名对象带来的方便,JSON.NET具有动态序列化和反序列化任意JSON内容的能力,不必将它映射到具体的强类型对象,它可以处理不确定的类型(集合.字典.动态对象和匿名对象 ...
- DotNetCore跨平台~Json动态序列化属性
回到目录 Json动态序列化属性,主要为了解决一个大实体,在返回前端时根据需要去序列化,如果实体里的某个属性在任务情况下都不序列化,可以添加[JsonIgnore]特性,这种是全局的过滤,但是更多的情 ...
- 【转载】C#使用Newtonsoft.Json组件来序列化对象
在Asp.Net网站开发的过程中,很多时候会遇到对象的序列化和反序列化操作,Newtonsoft.Json组件是专门用来序列化和反序列化操作的一个功能组件,引入这个DLL组件后,就可使用JsonCon ...
- C# dynamic类型序列化和反序列化之Newtonsoft.Json,动态解析远端返回的jSON数据
一.说明 1.Newtonsoft.Json 中的Linq To Json中提供了方便的json数据查询.修改等操作. 例如:JObject,JArray 2.在JObject.FromObject( ...
随机推荐
- 精通SpringBoot--整合Redis实现缓存
今天我们来讲讲怎么在spring boot 中整合redis 实现对数据库查询结果的缓存.首先第一步要做的就是在pom.xml文件添加spring-boot-starter-data-redis.要整 ...
- Codeforces Round #464 (Div. 2) B. Hamster Farm
B. Hamster Farm time limit per test2 seconds memory limit per test256 megabytes Problem Description ...
- Leetcode 872. 叶子相似的树
题目链接 https://leetcode-cn.com/problems/leaf-similar-trees/description/ 题目描述 请考虑一颗二叉树上所有的叶子,这些叶子的值按从左到 ...
- MySQL基础4-SQL简单查询(单表)
1.SELECT语句 2.运算符的优先级 利用Navicat中的查询方法: 栗子1:查询所有货品信息 栗子2:查询所有货品的id,productName,salePrice 当查询错误的时候出现的界面 ...
- Windows 2008 WinRM和WinRS能帮你做什么?
介绍 WinRM及WinRS更新包含在Windows Vista, Windows Server 2003 R2, Windows Server 2008 (及 Server 2008 Cor ...
- 【Generate Parentheses】cpp
题目: Given n pairs of parentheses, write a function to generate all combinations of well-formed paren ...
- ogre3D学习基础17 --- 如何手动创建ogre程序
建立自己的Ogre程序 一直以来都是使用ExampleApplication.h来写程序,现在来看看它到底有什么神奇的地方. 首先,我们新建一个win32空项目 然后配置环境 最后新建define.c ...
- 简单实现nodejs爬虫工具
约30行代码实现一个简单nodejs爬虫工具,定时抓取网页数据. 使用npm模块 request---简单http请求客户端.(轻量级) fs---nodejs文件模块. index.js var ...
- 01背包 HDU-1203
这道题在网上找的题解基本都是用min找出概率最小,然后用1减去的答案,我在这采用max来做,虽然只是换了公式,但是其中出现的问题还是想记录下. I NEED A OFFER! Time Limit: ...
- hdu6212[区间dp] 2017青岛ACM-ICPC网络赛
原题: BZOJ1032 (原题数据有问题) /*hdu6212[区间dp] 2017青岛ACM-ICPC网络赛*/ #include <bits/stdc++.h> using name ...