C#——JSON操作类简单封装(DataContractJsonSerializer)
Framework版本:.Net Framework 4
使用DataContractJsonSerializer时,实体请使用注解,格式如下
1、实体使用注解,并且提供get和set的public访问器
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.Serialization;
namespace ReligionServer.vo
{
[DataContract]
public class Inspection : Model.Inspection, IComparable<vo.Inspection>
{ [DataMember(Order = 0)]//Order表示输出顺序
public new string Time { get; set; } [IgnoreDataMember]//表示不参与Json转换
public String Msg { get; set; } public int Order { get; set; } [DataMember]
public String Period { get; set; } int IComparable<Inspection>.CompareTo(Inspection other)
{
int result;
//正序
if (this.RTime > other.RTime) {
result = ;
} else if (this.RTime < other.RTime) {
result = -;
}
else {
result = ;
}
//反序
if (this.Order == -)
{
if (result < ) {
result = System.Math.Abs(result);
} else if (result > ) {
result = - result;
}
} //我好蠢,这里做了排序,又对Service中对noCheckList做了分页(正序和反序分页) return result;
}
}
}
2、JSON工具源码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ReligionServer.util;
using System.Text;
using System.Runtime.Serialization.Json; namespace ReligionServer.util {
public class JsonUtil <T>{ private static DataContractJsonSerializer jsonSerializer;
private static T t; static JsonUtil(){
t = (T)ReflectionUtil.Instance(new JsonUtil<T>().GetType());
Console.WriteLine(t.GetType());
jsonSerializer = new DataContractJsonSerializer(typeof(T));
} public static String ObjToJson(T t){
using (System.IO.MemoryStream ms = new System.IO.MemoryStream()) {
//System.Diagnostics.Debug.WriteLine(t);
jsonSerializer.WriteObject(ms, t);
//System.Diagnostics.Debug.WriteLine(ms.ToArray().Length);
StringBuilder builder = new StringBuilder();
builder.Append(Encoding.UTF8.GetString(ms.ToArray()));
//System.Diagnostics.Debug.WriteLine(builder.Length);
//System.Diagnostics.Debug.WriteLine(builder.ToString());
return builder.ToString();
}
}
}
}
C#——JSON操作类简单封装(DataContractJsonSerializer)的更多相关文章
- C#——图片操作类简单封装
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Dr ...
- C#——文件操作类简单封装
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.IO ...
- C#封装的一个JSON操作类
using System; using System.Collections.Generic; using System.Collections; using System.Text; using S ...
- ASP.NET2.0 Newtonsoft.Json 操作类分享
JSON 是现在比较流行的数据交互格式,NET3.0+有自带类处理JSON,2.0的话需要借助Newtonsoft.Json来完成,不然自己写的话,很麻烦. 网上搜索下载 Newtonsoft.Jso ...
- MySQL操作类的封装(PHP)
<?php class mysql{ /** * 报错函数 * * @param string $error */ function err($error){ die("对不起,您的操 ...
- 公共的Json操作类
using System; using System.Data; using System.Text; using System.Collections.Generic; using System.R ...
- ADO.NET操作PostgreSQL:数据库操作类(已封装)
1.增.删.改通用方法 /// <summary> /// 增.删.改通用方法 /// </summary> /// <param name="commandT ...
- ADO.NET操作SQL Server:数据库操作类(已封装)
1.增.删.改通用方法 /// <summary> /// 增.删.改通用方法 /// </summary> /// <param name="commandT ...
- Java实体与Json操作类
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.Jav ...
随机推荐
- Linux 防火墙:Netfilter
一.Netfilter 简介 (1) Netfilter 是 Linux 内置的一种防火墙机制,我们一般也称之为数据包过滤机制,而 iptables 只是操作 netfilter 的一个命令行工具(2 ...
- interface Impl
public interface ActionBarOperations { void initSthOne(); void initSthTwo(); } public class ActionBa ...
- 微信小程序 --- 用户登录
整体逻辑:点击用户中心,如果如果整个页面没有
- 【转】stm32中断嵌套全攻略
断断续续学习STM32一学期了,时间过的好快,现在对STM32F103系列单片机的中断嵌套及外部中断做一个总结,全当学习笔记.废话不多说,ARM公司的Cortex-m3 内核,支持256个中断,其中包 ...
- 重新来认识你的老朋友Spring框架
欢迎查看Java开发之上帝之眼系列教程,如果您正在为Java后端庞大的体系所困扰,如果您正在为各种繁出不穷的技术和各种框架所迷茫,那么本系列文章将带您窥探Java庞大的体系.本系列教程希望您能站在上帝 ...
- LINQ中的连接(join)用法示例
Linq中连接主要有组连接.内连接.左外连接.交叉连接四种.各个用法如下. 1. 组连接 组连接是与分组查询是一样的.即根据分组得到结果. 如下例,根据publisther分组得到结果. 使用组连接的 ...
- square-and-multiply algorithm
https://en.wikipedia.org/wiki/Square-and-multiply_algorithm
- No module named pip.req
https://stackoverflow.com/questions/25192794/no-module-named-pip-req#
- 双态运维分享之:业务场景驱动的服务型CMDB
最近这几年,国内外CMDB失败的案例比比皆是,成功的寥寥可数,有人质疑CMDB is dead?但各种业务场景表明,当下数据中心运维,CMDB依然是不可或缺的一部分,它承载着运维的基础,掌握运维的命脉 ...
- 把大象装进冰箱的N种方法
作者:折剑头链接:https://www.zhihu.com/question/49214119/answer/115728034来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注 ...