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. C语言编程基础学习字符型数据的ASCII码值为何是负数?

    C语言是面向过程的,而C++是面向对象的 C和C++的区别: C是一个结构化语言,它的重点在于算法和数据结构.C程序的设计首要考虑的是如何通过一个过程,对输入(或环境条件)进行运算处理得到输出(或实现 ...

  2. linux 开机启动nginx

    这里使用的是编写shell脚本的方式来处理 vi /etc/init.d/nginx  (输入下面的代码) #!/bin/bash# nginx Startup script for the Ngin ...

  3. android基础---->DiskLruCache的使用及原理

    DiskLruCache是谷歌推荐的用来实现硬盘缓存的类,今天我们开始对于DiskLruCache的学习.DiskLruCache的测试代码:DiskLruCache的测试代码下载.关于FidkLru ...

  4. HTTP/2笔记之连接建立

    前言 HTTP/2协议在TCP连接之初进行协商通信,只有协商成功,才会涉及到后续的请求-响应等具体的业务型数据交换. HTTP版本标识符 h2,基于TLS之上构建的HTTP/2,作为ALPN的标识符, ...

  5. Maven常用操作

    1. 修改Maven的本地仓库路径 1.1 默认会放在~/.m2/repository目录下 (“~”代表用户的目录,比如windows下一般都是C:\Documents and Settings\[ ...

  6. Redis对于key的操作命令

     del key1 key2 ... Keyn 作用: 删除1个或多个键 返回值: 不存在的key忽略掉,返回真正删除的key的数量 rename key newkey 作用: 给key赋一个新的ke ...

  7. backup与recover

    完全恢复: 1.关闭DB2.拷贝文件3.启动DB.<出错>startup mount4.recover database until cancel using backup control ...

  8. 南京网络赛B-The writing on the wall

    30.43% 2000ms 262144K Feeling hungry, a cute hamster decides to order some take-away food (like frie ...

  9. c# BitArray 复制数组 copyto

    C# 点阵列(BitArray) BitArray 类管理一个紧凑型的位值数组,它使用布尔值来表示,其中 true 表示位是开启的(1),false 表示位是关闭的(0). C# 拷贝数组的几种方法

  10. Oracle管理监控之监控表空间使用率脚本

    SELECT D.TABLESPACE_NAME,       SPACE "SUM_SPACE(M)",       BLOCKS SUM_BLOCKS,       SPACE ...