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)的更多相关文章

  1. C#——图片操作类简单封装

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Dr ...

  2. C#——文件操作类简单封装

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.IO ...

  3. C#封装的一个JSON操作类

    using System; using System.Collections.Generic; using System.Collections; using System.Text; using S ...

  4. ASP.NET2.0 Newtonsoft.Json 操作类分享

    JSON 是现在比较流行的数据交互格式,NET3.0+有自带类处理JSON,2.0的话需要借助Newtonsoft.Json来完成,不然自己写的话,很麻烦. 网上搜索下载 Newtonsoft.Jso ...

  5. MySQL操作类的封装(PHP)

    <?php class mysql{ /** * 报错函数 * * @param string $error */ function err($error){ die("对不起,您的操 ...

  6. 公共的Json操作类

    using System; using System.Data; using System.Text; using System.Collections.Generic; using System.R ...

  7. ADO.NET操作PostgreSQL:数据库操作类(已封装)

    1.增.删.改通用方法 /// <summary> /// 增.删.改通用方法 /// </summary> /// <param name="commandT ...

  8. ADO.NET操作SQL Server:数据库操作类(已封装)

    1.增.删.改通用方法 /// <summary> /// 增.删.改通用方法 /// </summary> /// <param name="commandT ...

  9. Java实体与Json操作类

    import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.Jav ...

随机推荐

  1. NGUI屏幕自适应(转)

      屏幕自适应 NGUI可以比较方便的实现屏幕自适应,但是它的官方教程里面针对这个问题没有详细的教程,所以可能在实现的时候会走比较多的弯路.以下是我在开发过程中找到的一个比较方便的实现方法. 主要组件 ...

  2. Spring学习笔记--在SpEL中筛选集合

    要用到Spring的util(包括util:list等),xml文件中的beans中需要添加一些有关util的信息: <?xml version="1.0" encoding ...

  3. option 选不中问题

    function appAndBuz(appName,buzName,areaCenterCode){ //appName,buzName下拉框的值start $.ajax({ type: " ...

  4. saltstack远程执行命令.md

    saltstack 支持在master上向client 远程执行命令,并显示命令执行的结果 命令格式: salt '<操作目标>' <方法> [参数] 示例 示例1 ping检 ...

  5. c/c++左值和右值

    C/C++中的变量有左值和右值之分,他们的区别主要如下: (1)左值可以放在赋值号 = 的左右两边,右值只能放在赋值号 = 的右边 (2)在C语言中,有名字的变量即为左值:而函数的运行结果或表达式中间 ...

  6. 10.Node.js核心模块

    1.全局对象2.常用工具3.事件机制4.文件系统访问5.HTTP服务与客户端 一.全局对象(Global Object), 与window对象一样 process ==> 全局变量, 描述当前N ...

  7. jvm的内存模型

    转自:https://www.cnblogs.com/dingyingsi/p/3760447.html 我们知道,计算机CPU和内存的交互是最频繁的,内存是我们的高速缓存区,用户磁盘和CPU的交互, ...

  8. pyhton 函数参数,递归函数,高阶函数(一点点笔记)

    '''def test(x,y): print(x) print(y)test(2,y=3)def test(*args):#参数可以是不确定的多个数,接受N个位置参数,转换成元组形式 print(a ...

  9. xplan-打印执行顺序

    -- ------------------------------------------------------------------------------------------------- ...

  10. mysql中sql注入的随笔

    当使用如下登录代码时:就会引发sql注入问题 怎么注入呢? 'or 1=1 # 就可以了. 为什么呢? 首先or:在sql中是或者,只要满足前一个或后一个条件即可,只要所以不论你是 'or 1=1 # ...